Search in sources :

Example 1 with CannotCommitException

use of net.jini.core.transaction.CannotCommitException 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)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 RemoteException (java.rmi.RemoteException)1 Entry (net.jini.core.entry.Entry)1 CannotAbortException (net.jini.core.transaction.CannotAbortException)1 CannotCommitException (net.jini.core.transaction.CannotCommitException)1 Transaction (net.jini.core.transaction.Transaction)1 UnknownTransactionException (net.jini.core.transaction.UnknownTransactionException)1 Message (org.apache.camel.Message)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1