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();
}
}
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);
}
}
}
}
Aggregations