0%

WSDL

WSDL是WebService的一种描述语言,定义了webService的一些重要信息

  • 描述可公开使用的全部功能的信息
  • 这些功能的请求和响应消息的数据类型信息
  • 用于调用特定web服务的协议的绑定信息
  • 用于查找指定的web服务的地址的信息

构成元素

  • WSDL文档是一组定义,全部都是在 元素内部进行定义的,是WSDL文档的根元素
  • 描述web服务与服务用户之间交换消息的所有数据类型
  • 表示web服务与服务用户之间传递的数据的逻辑定义,包含0或多个消息元素,元素主要指请求参数或响应返回值
  • 该元素通过组合由定义的各种请求和响应消息,定义了web服务支持的各项操作的抽象定义,各操作均为输入消息和输出消息
  • 该元素指定用于表示通信中的特定元素定义的操作和消息的具体协议和数据格式
  • 该元素指定与web服务的绑定地址
  • 该元素聚集一组相关的元素,这些元素分别唯一指定web服务的绑定信息,包含多个元素的元素表示需通过多个绑定调用的服务功能
阅读全文 »

编解码器

网络传输中全部使用的是二进制流,而两边使用java对象进行发送和接收,就需要使用编解码器

编码器

将java对象转换为WebSocket消息

  • 对于文本消息,需要实现Encoder.Text
  • 对于二进制消息,需要实现Encoder.Binary
阅读全文 »

webSocket简介

WebSocket是一种应用协议,在TCP协议之上为通信双方提供全双工通信,在WebSocket应用中,服务器需要发布一个WebSocket端点,客户端使用这个端点的URI来连接服务器,在建立连接之后,WebSocket协议是对称的,客户端和服务器可以在连接打开的任何时间相互发送消息,而且可以在任何时间关闭连接。

客户端总是只能连接到一个服务器,而服务器可以接受多个客户端的连接

阅读全文 »

ChannelHandler组件分析

ChannelHandler主要是为了处理入站和出站数据的逻辑,netty中提供了多个接口

ChannelInboundHandler入站接口和ChannelOutboundHandler出站接口

入站和出站

  • ChannelInboundHandler入站表示数据是从远程主机到用户应用程序,处理进站数据和所有状态更改事件

  • ChannelOutboundHandler出站是指从用户应用程序到远程主机,处理出站数据,允许拦截各种操作

为了使数据从一端到达另一端,一个或多个ChannelHandler将以某种方式操作数据,这些Channel会添加到ChannelPipeline中,并按照被添加的顺序进行执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
*                                                 I/O Request
* via {@link Channel} or
* {@link ChannelHandlerContext}
* |
* +---------------------------------------------------+---------------+
* | ChannelPipeline | |
* | \|/ |
* | +---------------------+ +-----------+----------+ |
* | | Inbound Handler N | | Outbound Handler 1 | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ | |
* | | \|/ |
* | +----------+----------+ +-----------+----------+ |
* | | Inbound Handler N-1 | | Outbound Handler 2 | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ . |
* | . . |
* | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
* | [ method call] [method call] |
* | . . |
* | . \|/ |
* | +----------+----------+ +-----------+----------+ |
* | | Inbound Handler 2 | | Outbound Handler M-1 | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ | |
* | | \|/ |
* | +----------+----------+ +-----------+----------+ |
* | | Inbound Handler 1 | | Outbound Handler M | |
* | +----------+----------+ +-----------+----------+ |
* | /|\ | |
* +---------------+-----------------------------------+---------------+
* | \|/
* +---------------+-----------------------------------+---------------+
* | | | |
* | [ Socket.read() ] [ Socket.write() ] |
* | |
* | Netty Internal I/O Threads (Transport Implementation) |
* +-------------------------------------------------------------------+

ChannelInboundHandler接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public interface ChannelInboundHandler extends ChannelHandler {

/**
* The {@link Channel} of the {@link ChannelHandlerContext} was registered with its {@link EventLoop}
*/
void channelRegistered(ChannelHandlerContext ctx) throws Exception;

/**
* The {@link Channel} of the {@link ChannelHandlerContext} was unregistered from its {@link EventLoop}
*/
void channelUnregistered(ChannelHandlerContext ctx) throws Exception;

/**
* The {@link Channel} of the {@link ChannelHandlerContext} is now active
*/
// 连接被建立并准备进行通信时被调用
void channelActive(ChannelHandlerContext ctx) throws Exception;

/**
* The {@link Channel} of the {@link ChannelHandlerContext} was registered is now inactive and reached its
* end of lifetime.
*/
void channelInactive(ChannelHandlerContext ctx) throws Exception;

/**
* Invoked when the current {@link Channel} has read a message from the peer.
*/
// 读取当前channel的消息
void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception;

/**
* Invoked when the last message read by the current read operation has been consumed by
* {@link #channelRead(ChannelHandlerContext, Object)}. If {@link ChannelOption#AUTO_READ} is off, no further
* attempt to read an inbound data from the current {@link Channel} will be made until
* {@link ChannelHandlerContext#read()} is called.
*/
void channelReadComplete(ChannelHandlerContext ctx) throws Exception;

/**
* Gets called if an user event was triggered.
*/
void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception;

/**
* Gets called once the writable state of a {@link Channel} changed. You can check the state with
* {@link Channel#isWritable()}.
*/
void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception;

/**
* Gets called if a {@link Throwable} was thrown.
*/
// 当出现异常时调用该方法
@Override
@SuppressWarnings("deprecation")
void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception;
}

原子操作

在java.util.concurrent.atomic包下提供了很多原子操作类,多个线程执行一个操作时,其中任何一个线程要么完全执行此操作,要么没有执行此操作的任何步骤,其内部使用的CAS操作 乐观锁

以AtomicInteger为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class AtomicInteger extends Number implements java.io.Serializable {
private static final long serialVersionUID = 6214790243416807050L;

// setup to use Unsafe.compareAndSwapInt for updates
private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;

static {
try {
valueOffset = unsafe.objectFieldOffset
(AtomicInteger.class.getDeclaredField("value"));
} catch (Exception ex) { throw new Error(ex); }
}

private volatile int value;

// 获取当前值
public final int get() {
return value;
}

// 设置给定值
public final void set(int newValue) {
value = newValue;
}

// 延时设置
public final void lazySet(int newValue) {
unsafe.putOrderedInt(this, valueOffset, newValue);
}

// 以原子方式设置给定值,并返回旧值 线程安全版本的 tmp = oldValue; oldValue = newValue; return oldValue
public final int getAndSet(int newValue) {
return unsafe.getAndSetInt(this, valueOffset, newValue);
}

// 如果当前值等于预期值,则以原子方式设置为给定的更新值,成功则返回true,失败返回false
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}

// 如果当前值等于预期值,则以原子方式设置为给定的更新值,成功则返回true,失败返回false
// 与compareAndSet方法一样
public final boolean weakCompareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}

// 以原子方式加一 ,相当于线程安全版的i++
public final int getAndIncrement() {
return unsafe.getAndAddInt(this, valueOffset, 1);
}

// 以原子方式减一 ,相当于线程安全版的i--
public final int getAndDecrement() {
return unsafe.getAndAddInt(this, valueOffset, -1);
}

// 以原子方式将给定值与当前值相加 线程安全版的i=+10 先获取再相加
public final int getAndAdd(int delta) {
return unsafe.getAndAddInt(this, valueOffset, delta);
}

// 以原子方式加一 ,相当于线程安全版的++i
public final int incrementAndGet() {
return unsafe.getAndAddInt(this, valueOffset, 1) + 1;
}

// 以原子方式减一 ,相当于线程安全版的--i
public final int decrementAndGet() {
return unsafe.getAndAddInt(this, valueOffset, -1) - 1;
}

// 以原子方式将给定值与当前值相加 线程安全版的i+=10 先相加在获取
public final int addAndGet(int delta) {
return unsafe.getAndAddInt(this, valueOffset, delta) + delta;
}

}

指令重排序

JVM会根据处理器的特性适当的重新排序机器指令,使机器指令更符合CPU的执行特点,最大限度的发挥机器的性能,但是会导致执行顺序可能会与代码顺序不一致