Search in sources :

Example 1 with Transaction

use of net.jini.core.transaction.Transaction in project camel by apache.

the class JavaSpaceProducer method process.

public void process(Exchange exchange) throws Exception {
    Entry entry;
    Object body = exchange.getIn().getBody();
    if (!(body instanceof Entry)) {
        entry = new InEntry();
        if (body instanceof BeanInvocation) {
            ((InEntry) entry).correlationId = (new UID()).toString();
        }
        if (body instanceof byte[]) {
            ((InEntry) entry).binary = true;
            ((InEntry) entry).buffer = (byte[]) body;
        } else {
            ((InEntry) entry).binary = false;
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(body);
            ((InEntry) entry).buffer = bos.toByteArray();
        }
    } else {
        entry = (Entry) body;
    }
    Transaction tnx = null;
    if (transactionHelper != null) {
        tnx = transactionHelper.getJiniTransaction(transactionTimeout).transaction;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Writing body : " + entry);
    }
    javaSpace.write(entry, tnx, Lease.FOREVER);
    if (ExchangeHelper.isOutCapable(exchange)) {
        OutEntry tmpl = new OutEntry();
        tmpl.correlationId = ((InEntry) entry).correlationId;
        OutEntry replyCamelEntry = null;
        while (replyCamelEntry == null) {
            replyCamelEntry = (OutEntry) javaSpace.take(tmpl, tnx, 100);
        }
        Object obj;
        if (replyCamelEntry.binary) {
            obj = replyCamelEntry.buffer;
        } else {
            ByteArrayInputStream bis = new ByteArrayInputStream(replyCamelEntry.buffer);
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
        }
        exchange.getOut().setBody(obj);
    }
    if (tnx != null) {
        tnx.commit();
    }
}
Also used : UID(java.rmi.server.UID) Entry(net.jini.core.entry.Entry) Transaction(net.jini.core.transaction.Transaction) ByteArrayInputStream(java.io.ByteArrayInputStream) BeanInvocation(org.apache.camel.component.bean.BeanInvocation) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with Transaction

use of net.jini.core.transaction.Transaction in project camel by apache.

the class Task method run.

public void run() {
    Transaction tnx = null;
    try {
        DefaultExchange exchange = (DefaultExchange) endpoint.createExchange(ExchangePattern.InOut);
        Message message = exchange.getIn();
        if (transactionHelper != null) {
            tnx = transactionHelper.getJiniTransaction(transactionTimeout).transaction;
        }
        Entry entry = null;
        switch(verb) {
            case JavaSpaceConsumer.TAKE:
                entry = javaSpace.take(template, tnx, 100);
                break;
            case JavaSpaceConsumer.READ:
                entry = javaSpace.read(template, tnx, 100);
                break;
            default:
                throw new RuntimeCamelException("Wrong verb");
        }
        if (entry != null) {
            if (entry instanceof InEntry) {
                if (((InEntry) entry).binary) {
                    message.setBody(((InEntry) entry).buffer);
                } else {
                    ByteArrayInputStream bis = new ByteArrayInputStream(((InEntry) entry).buffer);
                    ObjectInputStream ois = new ObjectInputStream(bis);
                    Object obj = ois.readObject();
                    message.setBody(obj);
                }
                processor.process(exchange);
                Message out = exchange.getOut();
                if (out.getBody() != null && ExchangeHelper.isOutCapable(exchange)) {
                    OutEntry replyCamelEntry = new OutEntry();
                    replyCamelEntry.correlationId = ((InEntry) entry).correlationId;
                    if (out.getBody() instanceof byte[]) {
                        replyCamelEntry.binary = true;
                        replyCamelEntry.buffer = (byte[]) out.getBody();
                    } else {
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        ObjectOutputStream oos = new ObjectOutputStream(bos);
                        oos.writeObject(out.getBody());
                        replyCamelEntry.binary = false;
                        replyCamelEntry.buffer = bos.toByteArray();
                    }
                    javaSpace.write(replyCamelEntry, tnx, Lease.FOREVER);
                }
            } else {
                message.setBody(entry, Entry.class);
                processor.process(exchange);
            }
        }
    } catch (Exception e) {
        if (tnx != null) {
            try {
                tnx.abort();
            } catch (UnknownTransactionException e1) {
                throw new RuntimeCamelException(e1);
            } catch (CannotAbortException e1) {
                throw new RuntimeCamelException(e1);
            } catch (RemoteException e1) {
                throw new RuntimeCamelException(e1);
            }
        }
        throw new RuntimeCamelException(e);
    } finally {
        if (tnx != null) {
            try {
                tnx.commit();
            } catch (UnknownTransactionException e1) {
                throw new RuntimeCamelException(e1);
            } catch (RemoteException e1) {
                throw new RuntimeCamelException(e1);
            } catch (CannotCommitException e1) {
                throw new RuntimeCamelException(e1);
            }
        }
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) CannotAbortException(net.jini.core.transaction.CannotAbortException) Message(org.apache.camel.Message) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) UnknownTransactionException(net.jini.core.transaction.UnknownTransactionException) CannotAbortException(net.jini.core.transaction.CannotAbortException) RemoteException(java.rmi.RemoteException) CannotCommitException(net.jini.core.transaction.CannotCommitException) CannotCommitException(net.jini.core.transaction.CannotCommitException) Entry(net.jini.core.entry.Entry) Transaction(net.jini.core.transaction.Transaction) UnknownTransactionException(net.jini.core.transaction.UnknownTransactionException) ByteArrayInputStream(java.io.ByteArrayInputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RemoteException(java.rmi.RemoteException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Entry (net.jini.core.entry.Entry)2 Transaction (net.jini.core.transaction.Transaction)2 RemoteException (java.rmi.RemoteException)1 UID (java.rmi.server.UID)1 CannotAbortException (net.jini.core.transaction.CannotAbortException)1 CannotCommitException (net.jini.core.transaction.CannotCommitException)1 UnknownTransactionException (net.jini.core.transaction.UnknownTransactionException)1 Message (org.apache.camel.Message)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 BeanInvocation (org.apache.camel.component.bean.BeanInvocation)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1