Skip to content

Commit

Permalink
修复单例忘记将构造方法设置为private的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
songxinjianqwe committed Jul 29, 2018
1 parent c862c01 commit 34c35b8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* @author sinjinsong
* @date 2018/7/19
*/
@Slf4j
@AllArgsConstructor
@ChannelHandler.Sharable
public class HttpClientHandler extends ChannelInboundHandlerAdapter {
private Client client;
private ClientMessageConverter converter;

private static HttpClientHandler INSTANCE;

private HttpClientHandler(Client client, ClientMessageConverter converter) {
this.client = client;
this.converter = converter;
}

public synchronized static void init(Client client, ClientMessageConverter converter) {
if (INSTANCE == null) {
INSTANCE = new HttpClientHandler(client, converter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
* @author sinjinsong
* @date 2018/7/19
*/
@Slf4j
@AllArgsConstructor
@ChannelHandler.Sharable
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
private Server server;
private ServerMessageConverter converter;

private static HttpServerHandler INSTANCE;

private HttpServerHandler(Server server, ServerMessageConverter converter) {
this.server = server;
this.converter = converter;
}

public synchronized static void init(Server server,ServerMessageConverter converter) {
if (INSTANCE == null) {
INSTANCE = new HttpServerHandler(server,converter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;


Expand All @@ -15,11 +14,12 @@
*/
@Slf4j
@ChannelHandler.Sharable
@AllArgsConstructor
public class ToyClientHandler extends SimpleChannelInboundHandler<Message> {
private Client client;



private ToyClientHandler(Client client) {
this.client = client;
}
private static ToyClientHandler INSTANCE;

public synchronized static void init(Client client) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import static com.sinjinsong.toy.transport.api.domain.Message.PING;
Expand All @@ -17,12 +16,15 @@
* Created by SinjinSong on 2017/7/29.
* 实际的业务处理器,单例
*/
@AllArgsConstructor
@Slf4j
@ChannelHandler.Sharable
public class ToyServerHandler extends SimpleChannelInboundHandler<Message> {
private static ToyServerHandler INSTANCE;


private ToyServerHandler(Server server) {
this.server = server;
}

public synchronized static void init(Server server) {
if(INSTANCE == null) {
INSTANCE = new ToyServerHandler(server);
Expand Down

0 comments on commit 34c35b8

Please sign in to comment.