1919import java .io .DataOutputStream ;
2020import java .io .IOException ;
2121import java .nio .ByteBuffer ;
22+ import java .util .concurrent .CompletableFuture ;
2223import java .util .concurrent .locks .Lock ;
2324import java .util .concurrent .locks .ReentrantLock ;
2425
@@ -125,28 +126,30 @@ public boolean handleFrame(Frame f) throws IOException {
125126 * @param channel the channel on which to transmit the command
126127 * @throws IOException if an error is encountered
127128 */
128- public void transmit (AMQChannel channel ) throws IOException {
129+ public CompletableFuture < Void > transmit (AMQChannel channel ) throws IOException {
129130 int channelNumber = channel .getChannelNumber ();
130131 AMQConnection connection = channel .getConnection ();
131132
133+ CompletableFuture <Void > future = null ;
132134 assemblerLock .lock ();
133135 try {
134136 Method m = this .assembler .getMethod ();
135137 if (m .hasContent ()) {
136138 ByteBuffer bbBody = this .assembler .getByteBufferBody ();
137139 if (bbBody != null ) {
138- transmitWithByteBuffer (m , bbBody , channelNumber , connection );
140+ future = transmitWithByteBuffer (m , bbBody , channelNumber , connection );
139141 } else {
140142 transmitWithByteArray (m , this .assembler .getContentBody (), channelNumber , connection );
141143 }
142144 } else {
143- connection .writeFrame (m .toFrame (channelNumber ));
145+ connection .writeFrame (m .toFrame (channelNumber ), null );
144146 }
145147 } finally {
146148 assemblerLock .unlock ();
147149 }
148150
149151 connection .flush ();
152+ return future ;
150153 }
151154
152155 private void transmitWithByteArray (Method m , byte [] body , int channelNumber , AMQConnection connection ) throws IOException {
@@ -160,18 +163,18 @@ private void transmitWithByteArray(Method m, byte[] body, int channelNumber, AMQ
160163 String msg = String .format ("Content headers exceeded max frame size: %d > %d" , headerFrame .size (), frameMax );
161164 throw new IllegalArgumentException (msg );
162165 }
163- connection .writeFrame (m .toFrame (channelNumber ));
164- connection .writeFrame (headerFrame );
166+ connection .writeFrame (m .toFrame (channelNumber ), null );
167+ connection .writeFrame (headerFrame , null );
165168
166169 for (int offset = 0 ; offset < body .length ; offset += bodyPayloadMax ) {
167170 int remaining = body .length - offset ;
168171 int fragmentLength = (remaining < bodyPayloadMax ) ? remaining : bodyPayloadMax ;
169172 Frame frame = Frame .fromBodyFragment (channelNumber , body , offset , fragmentLength );
170- connection .writeFrame (frame );
173+ connection .writeFrame (frame , null );
171174 }
172175 }
173176
174- private void transmitWithByteBuffer (Method m , ByteBuffer body , int channelNumber , AMQConnection connection ) throws IOException {
177+ private CompletableFuture < Void > transmitWithByteBuffer (Method m , ByteBuffer body , int channelNumber , AMQConnection connection ) throws IOException {
175178 int bodySize = body .remaining ();
176179 Frame headerFrame = this .assembler .getContentHeader ().toFrame (channelNumber , bodySize );
177180
@@ -183,16 +186,23 @@ private void transmitWithByteBuffer(Method m, ByteBuffer body, int channelNumber
183186 String msg = String .format ("Content headers exceeded max frame size: %d > %d" , headerFrame .size (), frameMax );
184187 throw new IllegalArgumentException (msg );
185188 }
186- connection .writeFrame (m .toFrame (channelNumber ));
187- connection .writeFrame (headerFrame );
189+ connection .writeFrame (m .toFrame (channelNumber ), null );
190+ connection .writeFrame (headerFrame , null );
188191
192+ // TODO should complete the future in case of error
193+ CompletableFuture <Void > future = new CompletableFuture <>();
189194 int bodyPosition = body .position ();
190195 for (int offset = 0 ; offset < bodySize ; offset += bodyPayloadMax ) {
191196 int remaining = bodySize - offset ;
192197 int fragmentLength = (remaining < bodyPayloadMax ) ? remaining : bodyPayloadMax ;
193198 Frame frame = Frame .fromBodyFragment (channelNumber , body , bodyPosition + offset , fragmentLength );
194- connection .writeFrame (frame );
199+ if (offset + bodyPayloadMax >= bodySize ) {
200+ connection .writeFrame (frame , future );
201+ } else {
202+ connection .writeFrame (frame , null );
203+ }
195204 }
205+ return future ;
196206 }
197207
198208 @ Override public String toString () {
0 commit comments