forked from hornetq/hornetq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ClientProducerImpl.java
563 lines (446 loc) · 15 KB
/
ClientProducerImpl.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
/*
* Copyright 2009 Red Hat, Inc.
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.hornetq.core.client.impl;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicLong;
import org.hornetq.api.core.HornetQBuffer;
import org.hornetq.api.core.HornetQBuffers;
import org.hornetq.api.core.HornetQException;
import org.hornetq.api.core.Message;
import org.hornetq.api.core.SimpleString;
import org.hornetq.core.message.BodyEncoder;
import org.hornetq.core.message.impl.MessageInternal;
import org.hornetq.core.protocol.core.Channel;
import org.hornetq.core.protocol.core.impl.wireformat.SessionSendContinuationMessage;
import org.hornetq.core.protocol.core.impl.wireformat.SessionSendLargeMessage;
import org.hornetq.core.protocol.core.impl.wireformat.SessionSendMessage;
import org.hornetq.core.server.HornetQMessageBundle;
import org.hornetq.utils.DeflaterReader;
import org.hornetq.utils.HornetQBufferInputStream;
import org.hornetq.utils.TokenBucketLimiter;
import org.hornetq.utils.UUIDGenerator;
/**
* The client-side Producer connectionFactory class.
*
* @author <a href="mailto:tim.fox@jboss.com">Tim Fox</a>
* @author <a href="mailto:clebert.suconic@jboss.org">Clebert Suconic</a>
* @author <a href="mailto:ataylor@redhat.com">Andy Taylor</a>
* @version <tt>$Revision$</tt> $Id$
*/
public class ClientProducerImpl implements ClientProducerInternal
{
// Constants ------------------------------------------------------------------------------------
// Attributes -----------------------------------------------------------------------------------
private final SimpleString address;
private final ClientSessionInternal session;
private final Channel channel;
private volatile boolean closed;
// For rate throttling
private final TokenBucketLimiter rateLimiter;
private final boolean blockOnNonDurableSend;
private final boolean blockOnDurableSend;
private final SimpleString groupID;
private final int minLargeMessageSize;
private final ClientProducerCredits credits;
// Static ---------------------------------------------------------------------------------------
// Constructors ---------------------------------------------------------------------------------
public ClientProducerImpl(final ClientSessionInternal session,
final SimpleString address,
final TokenBucketLimiter rateLimiter,
final boolean blockOnNonDurableSend,
final boolean blockOnDurableSend,
final boolean autoGroup,
final SimpleString groupID,
final int minLargeMessageSize,
final Channel channel)
{
this.channel = channel;
this.session = session;
this.address = address;
this.rateLimiter = rateLimiter;
this.blockOnNonDurableSend = blockOnNonDurableSend;
this.blockOnDurableSend = blockOnDurableSend;
if (autoGroup)
{
this.groupID = UUIDGenerator.getInstance().generateSimpleStringUUID();
}
else
{
this.groupID = groupID;
}
this.minLargeMessageSize = minLargeMessageSize;
if (address != null)
{
credits = session.getCredits(address, false);
}
else
{
credits = null;
}
}
// ClientProducer implementation ----------------------------------------------------------------
public SimpleString getAddress()
{
return address;
}
public void send(final Message msg) throws HornetQException
{
checkClosed();
doSend(null, msg);
}
public void send(final SimpleString address, final Message msg) throws HornetQException
{
checkClosed();
doSend(address, msg);
}
public void send(final String address, final Message message) throws HornetQException
{
send(SimpleString.toSimpleString(address), message);
}
public synchronized void close() throws HornetQException
{
if (closed)
{
return;
}
doCleanup();
}
public void cleanUp()
{
if (closed)
{
return;
}
doCleanup();
}
public boolean isClosed()
{
return closed;
}
public boolean isBlockOnDurableSend()
{
return blockOnDurableSend;
}
public boolean isBlockOnNonDurableSend()
{
return blockOnNonDurableSend;
}
public int getMaxRate()
{
return rateLimiter == null ? -1 : rateLimiter.getRate();
}
// Public ---------------------------------------------------------------------------------------
public ClientProducerCredits getProducerCredits()
{
return credits;
}
// Protected ------------------------------------------------------------------------------------
// Package Private ------------------------------------------------------------------------------
// Private --------------------------------------------------------------------------------------
private void doCleanup()
{
if (address != null)
{
session.returnCredits(address);
}
session.removeProducer(this);
closed = true;
}
private void doSend(final SimpleString address, final Message msg) throws HornetQException
{
MessageInternal msgI = (MessageInternal)msg;
ClientProducerCredits theCredits;
boolean isLarge;
if (msgI.getBodyInputStream() != null || msgI.isLargeMessage() ||
msgI.getBodyBuffer().writerIndex() > minLargeMessageSize)
{
isLarge = true;
}
else
{
isLarge = false;
}
if (address != null)
{
if (!isLarge)
{
session.setAddress(msg, address);
}
else
{
msg.setAddress(address);
}
// Anonymous
theCredits = session.getCredits(address, true);
}
else
{
if (!isLarge)
{
session.setAddress(msg, this.address);
}
else
{
msg.setAddress(this.address);
}
theCredits = credits;
}
if (rateLimiter != null)
{
// Rate flow control
rateLimiter.limit();
}
if (groupID != null)
{
msgI.putStringProperty(Message.HDR_GROUP_ID, groupID);
}
boolean sendBlocking = msgI.isDurable() ? blockOnDurableSend : blockOnNonDurableSend;
session.workDone();
try
{
// This will block if credits are not available
// Note, that for a large message, the encode size only includes the properties + headers
// Not the continuations, but this is ok since we are only interested in limiting the amount of
// data in *memory* and continuations go straight to the disk
if (!isLarge)
{
theCredits.acquireCredits(msgI.getEncodeSize());
}
}
catch (InterruptedException e)
{
}
if (isLarge)
{
largeMessageSend(sendBlocking, msgI, theCredits);
}
else
{
SessionSendMessage packet = new SessionSendMessage(msgI, sendBlocking);
if (sendBlocking)
{
channel.sendBlocking(packet);
}
else
{
channel.sendBatched(packet);
}
}
}
private void checkClosed() throws HornetQException
{
if (closed)
{
throw HornetQMessageBundle.BUNDLE.producerClosed();
}
}
// Methods to send Large Messages----------------------------------------------------------------
/**
* @param msgI
* @throws HornetQException
*/
private void largeMessageSend(final boolean sendBlocking,
final MessageInternal msgI,
final ClientProducerCredits credits) throws HornetQException
{
if (session.isCompressLargeMessages())
{
msgI.putBooleanProperty(Message.HDR_LARGE_COMPRESSED, true);
}
int headerSize = msgI.getHeadersAndPropertiesEncodeSize();
if (msgI.getHeadersAndPropertiesEncodeSize() >= minLargeMessageSize)
{
throw HornetQMessageBundle.BUNDLE.headerSizeTooBig(headerSize);
}
// msg.getBody() could be Null on LargeServerMessage
if (msgI.getBodyInputStream() == null && msgI.getWholeBuffer() != null)
{
msgI.getWholeBuffer().readerIndex(0);
}
SessionSendLargeMessage initialChunk = new SessionSendLargeMessage(msgI);
channel.send(initialChunk);
try
{
credits.acquireCredits(msgI.getHeadersAndPropertiesEncodeSize());
}
catch (InterruptedException e)
{
}
InputStream input = msgI.getBodyInputStream();
if (msgI.isServerMessage())
{
largeMessageSendServer(sendBlocking, msgI, credits);
}
else if (input != null)
{
largeMessageSendStreamed(sendBlocking, msgI, input, credits);
}
else
{
largeMessageSendBuffered(sendBlocking, msgI, credits);
}
}
/**
* Used to send serverMessages through the bridges.
* No need to validate compression here since the message is only compressed at the client
* @param sendBlocking
* @param msgI
* @throws HornetQException
*/
private void largeMessageSendServer(final boolean sendBlocking,
final MessageInternal msgI,
final ClientProducerCredits credits) throws HornetQException
{
BodyEncoder context = msgI.getBodyEncoder();
final long bodySize = context.getLargeBodySize();
context.open();
try
{
for (int pos = 0; pos < bodySize;)
{
final boolean lastChunk;
final int chunkLength = Math.min((int)(bodySize - pos), minLargeMessageSize);
final HornetQBuffer bodyBuffer = HornetQBuffers.fixedBuffer(chunkLength);
context.encode(bodyBuffer, chunkLength);
pos += chunkLength;
lastChunk = pos >= bodySize;
final SessionSendContinuationMessage chunk = new SessionSendContinuationMessage(msgI,
bodyBuffer.toByteBuffer()
.array(),
!lastChunk,
lastChunk && sendBlocking);
if (sendBlocking && lastChunk)
{
// When sending it blocking, only the last chunk will be blocking.
channel.sendBlocking(chunk);
}
else
{
channel.send(chunk);
}
try
{
credits.acquireCredits(chunk.getPacketSize());
}
catch (InterruptedException e)
{
}
}
}
finally
{
context.close();
}
}
/**
* @param sendBlocking
* @param msgI
* @throws HornetQException
*/
private void largeMessageSendBuffered(final boolean sendBlocking,
final MessageInternal msgI,
final ClientProducerCredits credits) throws HornetQException
{
msgI.getBodyBuffer().readerIndex(0);
largeMessageSendStreamed(sendBlocking, msgI, new HornetQBufferInputStream(msgI.getBodyBuffer()), credits);
}
/**
* @param sendBlocking
* @param input
* @throws HornetQException
*/
private void largeMessageSendStreamed(final boolean sendBlocking,
final MessageInternal msgI,
final InputStream inputStreamParameter,
final ClientProducerCredits credits) throws HornetQException
{
boolean lastPacket = false;
InputStream input = inputStreamParameter;
// We won't know the real size of the message since we are compressing while reading the streaming.
// This counter will be passed to the deflater to be updated for every byte read
AtomicLong messageSize = new AtomicLong();
if (session.isCompressLargeMessages())
{
input = new DeflaterReader(inputStreamParameter, messageSize);
}
long totalSize = 0;
while (!lastPacket)
{
byte[] buff = new byte[minLargeMessageSize];
int pos = 0;
do
{
int numberOfBytesRead;
int wanted = minLargeMessageSize - pos;
try
{
numberOfBytesRead = input.read(buff, pos, wanted);
}
catch (IOException e)
{
throw HornetQMessageBundle.BUNDLE.errorReadingBody(e);
}
if (numberOfBytesRead == -1)
{
lastPacket = true;
break;
}
pos += numberOfBytesRead;
}
while (pos < minLargeMessageSize);
totalSize += pos;
final SessionSendContinuationMessage chunk;
if (lastPacket)
{
if (!session.isCompressLargeMessages())
{
messageSize.set(totalSize);
}
byte[] buff2 = new byte[pos];
System.arraycopy(buff, 0, buff2, 0, pos);
buff = buff2;
chunk = new SessionSendContinuationMessage(msgI, buff, false, sendBlocking, messageSize.get());
}
else
{
chunk = new SessionSendContinuationMessage(msgI, buff, true, false);
}
if (sendBlocking && lastPacket)
{
// When sending it blocking, only the last chunk will be blocking.
channel.sendBlocking(chunk);
}
else
{
channel.send(chunk);
}
try
{
credits.acquireCredits(chunk.getPacketSize());
}
catch (InterruptedException e)
{
}
}
try
{
input.close();
}
catch (IOException e)
{
throw HornetQMessageBundle.BUNDLE.errorClosingLargeMessage(e);
}
}
// Inner Classes --------------------------------------------------------------------------------
}