forked from rabbitmq/rabbitmq-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMQCommand.java
216 lines (188 loc) · 7.87 KB
/
AMQCommand.java
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2007-2023 VMware, Inc. or its affiliates. All rights reserved.
//
// This software, the RabbitMQ Java client library, is triple-licensed under the
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
// please see LICENSE-APACHE2.
//
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
// either express or implied. See the LICENSE file for specific language governing
// rights and limitations of this software.
//
// If you have any questions regarding licensing, please contact us at
package com.rabbitmq.client.impl;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.locks.ReentrantLock;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Command;
/**
* AMQP 0-9-1-specific implementation of {@link Command} which accumulates
* method, header and body from a series of frames, unless these are
* supplied at construction time.
* <h2>Concurrency</h2>
* This class is thread-safe.
*/
public class AMQCommand implements Command {
/** EMPTY_FRAME_SIZE = 8 = 1 + 2 + 4 + 1
* <ul><li>1 byte of frame type</li>
* <li>2 bytes of channel number</li>
* <li>4 bytes of frame payload length</li>
* <li>1 byte of payload trailer FRAME_END byte</li></ul>
* See {@link #checkEmptyFrameSize}, an assertion checked at
* startup.
*/
public static final int EMPTY_FRAME_SIZE = 8;
/** The assembler for this command - synchronised on - contains all the state */
private final CommandAssembler assembler;
private final ReentrantLock assemblerLock = new ReentrantLock();
AMQCommand(int maxBodyLength) {
this(null, null, null, maxBodyLength);
}
/** Construct a command ready to fill in by reading frames */
public AMQCommand() {
this(null, null, null, Integer.MAX_VALUE);
}
/**
* Construct a command with just a method, and without header or body.
* @param method the wrapped method
*/
public AMQCommand(com.rabbitmq.client.Method method) {
this(method, null, null, Integer.MAX_VALUE);
}
/**
* Construct a command with a specified method, header and body.
* @param method the wrapped method
* @param contentHeader the wrapped content header
* @param body the message body data
*/
public AMQCommand(com.rabbitmq.client.Method method, AMQContentHeader contentHeader, byte[] body) {
this.assembler = new CommandAssembler((Method) method, contentHeader, body, Integer.MAX_VALUE);
}
/**
* Construct a command with a specified method, header and body.
* @param method the wrapped method
* @param contentHeader the wrapped content header
* @param body the message body data
* @param maxBodyLength the maximum size for an inbound message body
*/
public AMQCommand(com.rabbitmq.client.Method method, AMQContentHeader contentHeader, byte[] body,
int maxBodyLength) {
this.assembler = new CommandAssembler((Method) method, contentHeader, body, maxBodyLength);
}
/** Public API - {@inheritDoc} */
@Override
public Method getMethod() {
return this.assembler.getMethod();
}
/** Public API - {@inheritDoc} */
@Override
public AMQContentHeader getContentHeader() {
return this.assembler.getContentHeader();
}
/** Public API - {@inheritDoc} */
@Override
public byte[] getContentBody() {
return this.assembler.getContentBody();
}
public boolean handleFrame(Frame f) throws IOException {
return this.assembler.handleFrame(f);
}
/**
* Sends this command down the named channel on the channel's
* connection, possibly in multiple frames.
* @param channel the channel on which to transmit the command
* @throws IOException if an error is encountered
*/
public void transmit(AMQChannel channel) throws IOException {
int channelNumber = channel.getChannelNumber();
AMQConnection connection = channel.getConnection();
assemblerLock.lock();
try {
Method m = this.assembler.getMethod();
if (m.hasContent()) {
byte[] body = this.assembler.getContentBody();
Frame headerFrame = this.assembler.getContentHeader().toFrame(channelNumber, body.length);
int frameMax = connection.getFrameMax();
boolean cappedFrameMax = frameMax > 0;
int bodyPayloadMax = cappedFrameMax ? frameMax - EMPTY_FRAME_SIZE : body.length;
if (cappedFrameMax && headerFrame.size() > frameMax) {
String msg = String.format("Content headers exceeded max frame size: %d > %d", headerFrame.size(), frameMax);
throw new IllegalArgumentException(msg);
}
connection.writeFrame(m.toFrame(channelNumber));
connection.writeFrame(headerFrame);
for (int offset = 0; offset < body.length; offset += bodyPayloadMax) {
int remaining = body.length - offset;
int fragmentLength = (remaining < bodyPayloadMax) ? remaining
: bodyPayloadMax;
Frame frame = Frame.fromBodyFragment(channelNumber, body,
offset, fragmentLength);
connection.writeFrame(frame);
}
} else {
connection.writeFrame(m.toFrame(channelNumber));
}
} finally {
assemblerLock.unlock();
}
connection.flush();
}
@Override public String toString() {
return toString(false);
}
public String toString(boolean suppressBody){
assemblerLock.lock();
try {
return new StringBuilder()
.append('{')
.append(this.assembler.getMethod())
.append(", ")
.append(this.assembler.getContentHeader())
.append(", ")
.append(contentBodyStringBuilder(
this.assembler.getContentBody(), suppressBody))
.append('}').toString();
} finally {
assemblerLock.unlock();
}
}
private static StringBuilder contentBodyStringBuilder(byte[] body, boolean suppressBody) {
try {
if (suppressBody) {
return new StringBuilder().append(body.length).append(" bytes of payload");
} else {
return new StringBuilder().append('\"').append(new String(body, "UTF-8")).append('\"');
}
} catch (Exception e) {
return new StringBuilder().append('|').append(body.length).append('|');
}
}
/** Called to check internal code assumptions. */
public static void checkPreconditions() {
checkEmptyFrameSize();
}
/**
* Since we're using a pre-computed value for EMPTY_FRAME_SIZE we
* check this is actually correct when run against the framing
* code in Frame.
*/
private static void checkEmptyFrameSize() {
Frame f = new Frame(AMQP.FRAME_BODY, 0, new byte[0]);
ByteArrayOutputStream s = new ByteArrayOutputStream();
try {
f.writeTo(new DataOutputStream(s));
} catch (IOException ioe) {
throw new IllegalStateException("IOException while checking EMPTY_FRAME_SIZE");
}
int actualLength = s.toByteArray().length;
if (EMPTY_FRAME_SIZE != actualLength) {
throw new IllegalStateException("Internal error: expected EMPTY_FRAME_SIZE("
+ EMPTY_FRAME_SIZE
+ ") is not equal to computed value: " + actualLength);
}
}
}