I am attaching the bean code. This includes some calls to a simple JDBC BC that
I have developed..
------------------------------------------------------------------------------------------------
public class BeanRouter implements MessageExchangeListener {
@Resource
DeliveryChannel channel;
@Resource
ComponentContext context;
public void setContext(ComponentContext context)
{
this.context=context;
}
public ComponentContext getContext()
{
return context;
}
// private static Map myMap = java.util.Collections.synchronizedMap(new
// HashMap());
// private static int key;
// private static final Log log = LogFactory.getLog(BeanRouter.class);
public void onMessageExchange(MessageExchange exchange)
throws MessagingException {
InOnly in = (InOnly) exchange;
if (in.getStatus() == ExchangeStatus.DONE) {
return;
} else if (in.getStatus() == ExchangeStatus.ERROR) {
return;
}
System.err.println(in.getService().getLocalPart() + " requested");
TransactionManager tm=null;
try {
NormalizedMessage message = exchange.getMessage("in");
Source content = message.getContent();
String inStr = getStringFromSource(content);
tm = (TransactionManager) getContext().getTransactionManager();
tm.begin();
// Camel Persist SU
String contentStr = DBLoggerTemplate.getDBXML("J20", "S20", true);
Source persistSource = new StreamSource(new java.io.StringReader(
contentStr));
callEndpointInOnlyMEP(
"http://servicemix.apache.org/samples/logging-sample",
"persist", persistSource);
// JDBC SP Call
String callSpInput = "<jdbc:input
xmlns:jdbc='http://servicemix.apache.org/jdbc/1.0' "
+ "operationType='SimpleSP' "
+ "sql='call sp_audit(?,?,?,?)' "
+ "paramValues ='01,S02,2007-02-11,DONE' />";
Source spSource = new StreamSource(new java.io.StringReader(
callSpInput));
callEndpointInOnlyMEP(
"http://servicemix.apache.org/samples/logging-sample",
"jdbcService", spSource);
// JDBC Encoded Insert
String encodedObject = (String) Base64Util.encodeObject(inStr);
String callInsert = "<jdbc:input
xmlns:jdbc='http://servicemix.apache.org/jdbc/1.0' "
+ "operationType='Insert' "
+ "sql='insert into ncoarequest values(?,?)' "
+ "paramValues ='ID01," + encodedObject + "' />";
Source insertSource = new StreamSource(new java.io.StringReader(
callInsert));
callEndpointInOnlyMEP(
"http://servicemix.apache.org/samples/logging-sample",
"jdbcService", insertSource);
// JDBC Decoded Query
String selectInput = "<jdbc:input
xmlns:jdbc='http://servicemix.apache.org/jdbc/1.0' "
+ "operationType='SIMPLEQUERY' "
+ "sql='select reqXML from ncoarequest where id=?' "
+ "returnType='STRING' " + "paramValues ='ID01' />";
Source selectSource = new StreamSource(new java.io.StringReader(
selectInput));
Source resultSource = callEndpointInOutMEP(
"http://servicemix.apache.org/samples/logging-sample",
"jdbcService", selectSource);
String selectedString = getStringFromSource(resultSource);
String encodedXML = getXPathValue("//*/ResultSet/text()",
selectedString);
String decodedString = (String) Base64Util
.decodeToObject(encodedXML);
System.err.println("Output from jdbc call:" + decodedString);
// JDBC Delete
String callDelete = "<jdbc:input
xmlns:jdbc='http://servicemix.apache.org/jdbc/1.0' "
+ "operationType='Delete' "
+ "sql='delete from ncoarequest where id=?' "
+ "paramValues ='ID01'/>";
Source deleteSource = new StreamSource(new java.io.StringReader(
callDelete));
callEndpointInOnlyMEP(
"http://servicemix.apache.org/samples/logging-sample",
"jdbcService", deleteSource);
exchange.setStatus(ExchangeStatus.DONE);
channel.send(exchange);
tm.commit();
} catch (Exception e) {
try
{
System.err.println("Rollbacking Transaction");
tm.rollback();
}catch(Exception ex)
{
}
exchange.setError(e);
channel.send(exchange);
}
}
//Helper Method - In MEP call
private void callEndpointInOnlyMEP(String namespace, String serviceName,
Source source) throws Exception {
QName targetService = new QName(namespace, serviceName);
MessageExchangeFactory exchangeFact = channel
.createExchangeFactoryForService(targetService);
InOnly exchange = exchangeFact.createInOnlyExchange();
NormalizedMessage message = exchange.createMessage();
message.setContent(source);
exchange.setInMessage(message);
channel.sendSync(exchange);
if (exchange.getStatus() == ExchangeStatus.ERROR) {
Exception e = exchange.getError();
if (e == null) {
e = new JBIException("Unkown error");
}
throw e;
}
}
//Helper Method - In-Out MEP call
private Source callEndpointInOutMEP(String namespace, String serviceName,
Source source) throws Exception {
QName targetService = new QName(namespace, serviceName);
MessageExchangeFactory exchangeFact = channel
.createExchangeFactoryForService(targetService);
InOut exchange = exchangeFact.createInOutExchange();
NormalizedMessage message = exchange.createMessage();
message.setContent(source);
exchange.setInMessage(message);
channel.sendSync(exchange);
if (exchange.getStatus() == ExchangeStatus.ERROR) {
Exception e = exchange.getError();
if (e == null) {
e = new JBIException("Unkown error");
}
throw e;
}
return exchange.getMessage("out").getContent();
}
//Helper String from Source
private String getStringFromSource(Source source) throws Exception {
StringWriter stringWriter = new StringWriter();
Result result = new StreamResult(stringWriter);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(source, result);
return stringWriter.getBuffer().toString();
}
//Helper to get xPath Value
private String getXPathValue(String xPath, String selectedString)
throws Exception {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
docFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = docFactory.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new java.io.StringReader(
selectedString)));
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(xPath);
return (String) expr.evaluate(doc, XPathConstants.STRING);
}
}
------------------------------------------------------------------------------------------------
Regards,
Rabi Mishra
Wipro Technologies
Hinjewadi, Pune
Cell: +91(0)9765391877
http://rabisblog.blogspot.com/
________________________________
From: Guillaume Nodet [mailto:[EMAIL PROTECTED]
Sent: Tue 4/8/2008 9:13 PM
To: [email protected]
Subject: Re: Transaction in a Bean SU
Maybe you could create a junit test so that we can have a look at the code
used ?
On Tue, Apr 8, 2008 at 5:31 PM, <[EMAIL PROTECTED]> wrote:
> oops... sorry about the typo..
>
> Yes, I could not control the transaction. I am calling these endpoints
> using sendSync() as there is a particular sequence of calling these
> endpoints, so I am calling them synchronously.....
>
> ________________________________
>
> From: Bruce Snyder [mailto:[EMAIL PROTECTED]
> Sent: Tue 4/8/2008 8:57 PM
> To: [email protected]
> Subject: Re: Transaction in a Bean SU
>
>
>
> On Tue, Apr 8, 2008 at 2:32 PM, <[EMAIL PROTECTED]> wrote:
> > Thanx Bruce.. I could get the transaction manager this way.
>
> Great :-).
>
> > But I could manage to control the transaction with multipe endpoints
> > with it.
>
> Do you mean that you *cannot* control the transaction?
>
> > Is this because this is the transaction manager in the component
> > context?? Does JBI support transaction rollbacks with endpoints??
>
> From within the servicemix-bean POJO, to send messages to these other
> endpoints, are you calling send() or sendSync()?
>
> Bruce
> --
> perl -e 'print
> unpack("u30","D0G)[EMAIL PROTECTED]&5R\"F)R=6-E+G-N>61E<D\!G;6%I;\"YC;VT*"
> );'
>
> Apache ActiveMQ - http://activemq.org/
> Apache Camel - http://activemq.org/camel/
> Apache ServiceMix - http://servicemix.org/
> Apache Geronimo - http://geronimo.apache.org/
>
> Blog: http://bruceblog.org/
>
>
>
> The information contained in this electronic message and any attachments
> to this message are intended for the exclusive use of the addressee(s) and
> may contain proprietary, confidential or privileged information. If you are
> not the intended recipient, you should not disseminate, distribute or copy
> this e-mail. Please notify the sender immediately and destroy all copies of
> this message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The recipient
> should check this email and any attachments for the presence of viruses. The
> company accepts no liability for any damage caused by any virus transmitted
> by this email.
>
> www.wipro.com
>
>
--
Cheers,
Guillaume Nodet
------------------------
Blog: http://gnodet.blogspot.com/
The information contained in this electronic message and any attachments to
this message are intended for the exclusive use of the addressee(s) and may
contain proprietary, confidential or privileged information. If you are not the
intended recipient, you should not disseminate, distribute or copy this e-mail.
Please notify the sender immediately and destroy all copies of this message and
any attachments.
WARNING: Computer viruses can be transmitted via email. The recipient should
check this email and any attachments for the presence of viruses. The company
accepts no liability for any damage caused by any virus transmitted by this
email.
www.wipro.com