Search in sources :

Example 1 with BufferEditor

use of org.fusesource.hawtbuf.BufferEditor in project fabric8 by jboss-fuse.

the class ClientInvokerImpl method request.

protected Object request(ProxyInvocationHandler handler, final String address, final UTF8Buffer service, final ClassLoader classLoader, final Method method, final Object[] args) throws Exception {
    if (!running.get()) {
        throw new IllegalStateException("DOSGi Client stopped");
    }
    final long correlation = correlationGenerator.incrementAndGet();
    // Encode the request before we try to pass it onto
    // IO layers so that #1 we can report encoding error back to the caller
    // and #2 reduce CPU load done in the execution queue since it's
    // serially executed.
    DataByteArrayOutputStream baos = new DataByteArrayOutputStream((int) (handler.lastRequestSize * 1.10));
    // we don't know the size yet...
    baos.writeInt(0);
    baos.writeVarLong(correlation);
    writeBuffer(baos, service);
    MethodData methodData = getMethodData(method);
    writeBuffer(baos, methodData.signature);
    final ResponseFuture future = methodData.invocationStrategy.request(methodData.serializationStrategy, classLoader, method, args, baos);
    // toBuffer() is better than toByteArray() since it avoids an
    // array copy.
    final Buffer command = baos.toBuffer();
    // Update the field size.
    BufferEditor editor = command.buffer().bigEndianEditor();
    editor.writeInt(command.length);
    handler.lastRequestSize = command.length;
    queue().execute(new Runnable() {

        public void run() {
            try {
                TransportPool pool = transports.get(address);
                if (pool == null) {
                    pool = new InvokerTransportPool(address, queue());
                    transports.put(address, pool);
                    pool.start();
                }
                requests.put(correlation, future);
                pool.offer(command, correlation);
            } catch (Exception e) {
                LOGGER.info("Error while sending request", e);
                future.fail(e);
            }
        }
    });
    // TODO: make that configurable, that's only for tests
    return future.get(timeout, TimeUnit.MILLISECONDS);
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) DataByteArrayOutputStream(org.fusesource.hawtbuf.DataByteArrayOutputStream) BufferEditor(org.fusesource.hawtbuf.BufferEditor) IOException(java.io.IOException)

Example 2 with BufferEditor

use of org.fusesource.hawtbuf.BufferEditor in project fabric8 by jboss-fuse.

the class ActiveMQBytesMessage method storeContent.

private void storeContent() {
    try {
        if (dataOut != null) {
            dataOut.close();
            Buffer bs = bytesOut.toBuffer();
            if (compressed) {
                int pos = bs.offset;
                bs.offset = 0;
                BufferEditor e = BufferEditor.big(bs);
                e.writeInt(length);
                bs.offset = pos;
            }
            setContent(bs);
            bytesOut = null;
            dataOut = null;
        }
    } catch (IOException ioe) {
        // TODO verify
        throw new RuntimeException(ioe.getMessage(), ioe);
    // RuntimeException
    }
}
Also used : Buffer(org.fusesource.hawtbuf.Buffer) BufferEditor(org.fusesource.hawtbuf.BufferEditor)

Aggregations

Buffer (org.fusesource.hawtbuf.Buffer)2 BufferEditor (org.fusesource.hawtbuf.BufferEditor)2 IOException (java.io.IOException)1 DataByteArrayOutputStream (org.fusesource.hawtbuf.DataByteArrayOutputStream)1 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)1