Search in sources :

Example 1 with Dispatched

use of io.fabric8.dosgi.api.Dispatched in project fabric8 by jboss-fuse.

the class ServerInvokerImpl method onCommand.

protected void onCommand(final Transport transport, Object data) {
    try {
        final DataByteArrayInputStream bais = new DataByteArrayInputStream((Buffer) data);
        final int size = bais.readInt();
        final long correlation = bais.readVarLong();
        // Use UTF8Buffer instead of string to avoid encoding/decoding UTF-8 strings
        // for every request.
        final UTF8Buffer service = readBuffer(bais).utf8();
        final Buffer encoded_method = readBuffer(bais);
        final ServiceFactoryHolder holder = holders.get(service);
        final MethodData methodData = holder.getMethodData(encoded_method);
        final Object svc = holder.factory.get();
        Runnable task = new Runnable() {

            public void run() {
                final DataByteArrayOutputStream baos = new DataByteArrayOutputStream();
                try {
                    // make space for the size field.
                    baos.writeInt(0);
                    baos.writeVarLong(correlation);
                } catch (IOException e) {
                    // should not happen
                    throw new RuntimeException(e);
                }
                // Lets decode the remaining args on the target's executor
                // to take cpu load off the
                methodData.invocationStrategy.service(methodData.serializationStrategy, holder.loader, methodData.method, svc, bais, baos, new Runnable() {

                    public void run() {
                        holder.factory.unget();
                        final Buffer command = baos.toBuffer();
                        // Update the size field.
                        BufferEditor editor = command.buffer().bigEndianEditor();
                        editor.writeInt(command.length);
                        queue().execute(new Runnable() {

                            public void run() {
                                transport.offer(command);
                            }
                        });
                    }
                });
            }
        };
        Executor executor;
        if (svc instanceof Dispatched) {
            executor = ((Dispatched) svc).queue();
        } else {
            executor = blockingExecutor;
        }
        executor.execute(task);
    } catch (Exception e) {
        LOGGER.info("Error while reading request", e);
    }
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) EOFException(java.io.EOFException) Dispatched(io.fabric8.dosgi.api.Dispatched) Executor(java.util.concurrent.Executor)

Aggregations

Dispatched (io.fabric8.dosgi.api.Dispatched)1 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 Executor (java.util.concurrent.Executor)1