Spring集成示例组件实战

一、背景描述

我构想了一个类似于mybatis的简化场景如下:

假设当前我有一个组件,名字是Mine。其功能是读取XML文件封装为内存对象,并提供一些操作,如读写等。现在我想通过Spring集成该组件。达到通过自定义注解的属性指定其绑定的XML文件,然后通过FactoryBean解析生成真正的目标对象,然后被纳入Spring的Bean管理中。使其可以通过Spring的注解(如Autowire等)注入到其他Bean里。

阅读更多

零拷贝

零、零拷贝概念

Zero-copy“ describes computer operations in which the CPU does not perform the task of copying data from one memory area to another. —— wikipedia

如维基百科所说,零拷贝是指避免CPU在不同的内存区域间拷贝数据。这可具体细分为操作系统级(OS level)别和用户态(User level)级别。

阅读更多

Netty高性能分析

一、Netty高性能分析

1)使用NIO多路复用技术,使其能高效的支持大量的客户端连接

2)使用主从Reactor模型,BossGroup和WorkerGroup分工明确,BossGroup只负责接收Channel,加快了客户端建立连接的速度。

阅读更多

Netty核心组件源码分析

以下结构图和流程图通过阅读netty-all-4.0.19.Final.jar源码分析而来。本文主要是以Server端执行过程进行分析。

零、Netty使用示例代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ChatServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();

ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new DelimiterBasedFrameDecoder(MAX_FRAME_LENGTH, DELIMITER));
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new ChatServerHandler());
}
});

ChannelFuture channelFuture = serverBootstrap.bind(LISTEN_PORT).sync();
System.out.println("ChatServer started.");
channelFuture.channel().closeFuture().await();
}
}
阅读更多

Netty聊天室Demo

一、概述

本文使用Netty实现了简单的聊天逻辑,旨在理解Netty的API使用,粘包拆包,编解码,处理器链的内容。

应用层需要对接收到的TCP数据包的粘包拆包情况进行必要处理,粘包拆包如下图:

阅读更多

Springboot自动配置

一、SpringBoot自动配置原理

简单的SpingBoot启动示例:

1
2
3
4
5
6
@SpringBootApplication
public class SpringBootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootdemoApplication.class, args);
}
}
阅读更多

Scalable_IO_in_Java

本文是Scalable IO in Java的翻译,旨在了解java中IO的React模式。为了更清晰的理解,我调整了文章的部分顺序。

大纲

  • 可扩展的网络服务
阅读更多

IO

一、IO概念

IO是计算机程序和磁盘,网卡等外部资源进行交互的接口。而这些敏感资源都是由操作系统进行统一管理的,所有计算机语言的IO操作最终都是通过操作系统提供的系统调用函数来实现的。

image-20210124155035560

阅读更多

Spring集成第三方组件-Mybatis

一、mybatis简介

Mybaits是java语言一个ORM框架。官网介绍如下:

MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。MyBatis 可以通过简单的 XML 或注解来配置和映射原始类型、接口和 Java POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录。

阅读更多

Spring集成第三方组件-JSF

一、JSF介绍

jsf是京东内部自研使用的RPC框架,同dubbo,目前已开源至github—joyrpc。本文从spring的角度去看是如何集成jsf的。

二、Spring在配置文件支持自定义namespace

阅读更多

断言

一、业务代码中禁用Assert

  • assert在非测试执行时是默认关闭的,需要加-ea参数才能开启;
  • 避免非测试用例中写assert,不然会被自己蠢哭;
阅读更多

DependencyManagment作用

介绍

Dependency management:this allows project authors to directly specify the versions of artifacts to be used when they are encountered in transitive dependencies or in dependencies where no version has been specified.

阅读更多

误用BlockingQueue方法导致日志丢失

一、背景

最近研发反映线上EDI的日志有丢失现象,经过审查代码发现,本机使用的BlockingQueue方法有误,使用的BlockingQueue#add方法,此时当队列满时会插入失败,返回false。
EDI运行时持久化逻辑

阅读更多

System.currentTimeMillis()与GMT

一、System.currentTimeMillis()函数注释

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public final class System {
/**
* Returns the current time in milliseconds. Note that
* while the unit of time of the return value is a millisecond,
* the granularity of the value depends on the underlying
* operating system and may be larger. For example, many
* operating systems measure time in units of tens of
* milliseconds.
*
* <p> See the description of the class <code>Date</code> for
* a discussion of slight discrepancies that may arise between
* "computer time" and coordinated universal time (UTC).
*
* @return the difference, measured in milliseconds, between
* the current time and midnight, January 1, 1970 UTC.
* @see java.util.Date
*/
public static native long currentTimeMillis();
}
阅读更多