最后
金三银四到了,送上一个小福利!
channels.forEach(channel -> {
if(channel == ctx.channel()){
String sendMsg = String.format(message, “我”);
channel.writeAndFlush(sendMsg);
System.out.println(sendMsg);
}else{
String sendMsg = String.format(message, ctx.channel().remoteAddress());
channel.writeAndFlush(sendMsg);
System.out.println(sendMsg);
}
});
}
//断开连接 , 把消息广播给其他客户端
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
String message = dateFormat.format(new Date())+“:”+ctx.channel().remoteAddress()+“:断开连接”;
channels.writeAndFlush(message);
System.out.println(message);
}
//建立连接
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
String message = dateFormat.format(new Date())+“:”+ctx.channel().remoteAddress()+“:加入聊天室”;
//自动把消息广播给其客户端
channels.writeAndFlush(message);
//客户端加入组
channels.add(ctx.channel());
System.out.println(message);
}
//出现异常
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.channel().close();
}
//客户端退出
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String message = dateFormat.format(new Date())+“:”+ctx.channel().remoteAddress()+“:退出聊天室”;
System.out.println(message);
}
//客户端出于活动
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
String message = dateFormat.format(new Date())+“:”+ctx.channel().remoteAddress()+“:上线啦”;
System.out.println(message);
}
}
=================================================================
客户端需要创建一个循环事件组,然后通过BootStrap去启动,然后扫描键盘输入作为数据源来把信息发送给服务端
public class NettyGroupChatClient {
public static void main(String[] args) {
new NettyGroupChatClient().start();
}
public void start(){
NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer() {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
//解码器
channel.pipeline().addLast(“decoder”, new StringDecoder());
//编码器
channel.pipeline().addLast(“encoder”,new StringEncoder());
//处理器
channel.pipeline().addLast(“nettyGroupChatClientHandler”,new NettyGroupChatClientHandler());
}
});
try {
ChannelFuture future = bootstrap.connect(new InetSocketAddress(“127.0.0.1”, 8888)).sync();
//通道
Channel channel = future.channel();
//扫描键盘输入
Scanner scanner = new Scanner(System.in);
while(scanner.hasNextLine()){
String message = scanner.nextLine();
//发送数据
channel.writeAndFlush(message);
}
} catch (InterruptedException e) {
}finally {
eventLoopGroup.shutdownGracefully();
}
}
}
对于处理器只需要接收服务端转发过来的消息即可
public class NettyGroupChatClientHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
System.out.println(msg);
}
}
启动服务端和多个客户端,效果如下
总结
蚂蚁面试比较重视基础,所以Java那些基本功一定要扎实。蚂蚁的工作环境还是挺赞的,因为我面的是稳定性保障部门,还有许多单独的小组,什么三年1班,很有青春的感觉。面试官基本水平都比较高,基本都P7以上,除了基础还问了不少架构设计方面的问题,收获还是挺大的。
经历这次面试我还通过一些渠道发现了需要大厂真实面试主要有:蚂蚁金服、拼多多、阿里云、百度、唯品会、携程、丰巢科技、乐信、软通动力、OPPO、银盛支付、中国平安等初,中级,高级Java面试题集合,附带超详细答案,希望能帮助到大家。
家。
[外链图片转存中…(img-toWTzwVc-1715427164603)]