Author |
Message
|
afielden |
Posted: Thu Sep 30, 2010 6:12 am Post subject: How to access message content when no message set is defined |
|
|
Novice
Joined: 30 Sep 2010 Posts: 10
|
I have a very simple message flow consisting of 3 nodes:-
1. FileInput - reads a plain text file
2. JavaCompute - processes the file contents
3. FileOutput - writes the processed contents to an output file
I have no message set defined, as the input file contains arbitrary text.
My question is: How do I access the contents of the text file from within the JavaCompute node?
I've tried various things, but had hoped that assembly.getMessage() may deliver what I wanted, however it doesn't.
Any ideas how I could access the file contents please?
here's my current code:-
Code: |
public void evaluate(MbMessageAssembly assembly) throws MbException
{
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage message = assembly.getMessage();
MbElement element = message.getRootElement();
MbElement child = element.getFirstChild();
String value = child.getValueAsString();
// ----------------------------------------------------------
// Add user code below
// End of user code
// ----------------------------------------------------------
// The following should only be changed
// if not propagating message to the 'out' terminal
out.propagate(assembly);
}
|
|
|
Back to top |
|
 |
fjb_saper |
Posted: Thu Sep 30, 2010 6:17 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
You need to look at samples and get training.
Knowing Java is not enough for this type of task.
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
WMBDEV1 |
Posted: Thu Sep 30, 2010 6:20 am Post subject: Re: How to access message content when no message set is def |
|
|
Sentinel
Joined: 05 Mar 2009 Posts: 888 Location: UK
|
|
Back to top |
|
 |
lancelotlinc |
Posted: Thu Sep 30, 2010 6:32 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
Second fjb_saper's comment: WebSphere Message Broker is not a pick-it-up-as-you-learn kind of environment. You need VM663 class (or equivalent).
http://www-304.ibm.com/jct03001c/services/learning/ites.wss/us/en?pageType=course_description&courseCode=VM663
I respectfully disagree with my esteemed collegue WMBDEV1. ESQL can do it, but JCNs are the way to go in my humble opinion.
Here is the code that will do what you want:
Code: |
try{
MbMessage message = assembly.getMessage();
// Walk the cursor down the tree to find the data.
MbElement cursor = null;
cursor = message.getRootElement(); // Root element.
cursor = cursor.getFirstChild(); // Properties element.
cursor = cursor.getNextSibling(); // MQMD element.
cursor = cursor.getNextSibling(); // MQMD.Blob element.
cursor = cursor.getFirstChild(); // Parser element.
cursor = cursor.getNextSibling(); // Parser.Blob element.
byte[] textIn = ((byte[]) cursor.getValue());
String textOut = new String(textIn);
logger.info(textOut);
} catch (Exception e ){}
|
This code spits out to a log4j file the contents of a message.
But again you need training. Don't run with scissors. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
zpat |
Posted: Thu Sep 30, 2010 7:12 am Post subject: |
|
|
 Jedi Council
Joined: 19 May 2001 Posts: 5866 Location: UK
|
I prefer ESQL. Just set the message type to BLOB in the input node.
You can then cast the blob to whatever codepage you want in the compute node.
Once you have a string to examine - all the many powerful string functions in ESQL are open to you. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Thu Sep 30, 2010 7:31 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
zpat wrote: |
I prefer ESQL. Just set the message type to BLOB in the input node.
You can then cast the blob to whatever codepage you want in the compute node.
Once you have a string to examine - all the many powerful string functions in ESQL are open to you. |
Accessing the data is only half the puzzle, as proposed by the original Poster.
Yes, it is possible to output data to log4j from ESQL; however, it is more convenient to do it from JCN.
This is a philosophical point. The two forms of compute nodes are not mutually exclusive. You can use either or both.
I advocate JCNs because, as you get more intricate and complex in coding, I find Java a more nimble, flexible and better supported language in general than ESQL.
Philosophically, I see ESQL's value as helping Database Administrators be quickly up-to-speed in Message Broker. Complicated contact admin with code - it seems to me - is better in Java (ie. lots of branching, conditional parsing/modification of payload data, intricate logic). It is true, you can do everything in ESQL (almost) that you can do in Java. Even call Java functions from ESQL.
That being said, I am not zealously saying in a dogmatic way one or the other. I think each to his own. If you like ESQL, more power to you. If you like Java, more power to you. More power to the whole world. ESQL-ers and Java-ites can live peaceably with all mankind, as much as lieth in us.
Peace! _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
fjb_saper |
Posted: Thu Sep 30, 2010 7:43 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
I can follow zpat
Your full paragraph of Java code would be replaced by a one liner...
Code: |
SET myBLOB = InputRoot.BLOB.BLOB; |
Makes it sometimes also a little bit easier to read...
However as said there is nothing wrong in doing it either way.  _________________ MQ & Broker admin |
|
Back to top |
|
 |
WMBDEV1 |
Posted: Thu Sep 30, 2010 7:46 am Post subject: |
|
|
Sentinel
Joined: 05 Mar 2009 Posts: 888 Location: UK
|
I deliberatly didnt respond earlier because the compute vs jcn debate has been discussed loads of time before (and I wont be drawn on it again now).
lancelotlinc wrote: |
Yes, it is possible to output data to log4j from ESQL; however, it is more convenient to do it from JCN.
|
I'm interesting in knowing where the obsession with log4j came from, I've not seen this as a requirement frmo the OP.
Quote: |
I advocate JCNs because, as you get more intricate and complex in coding, I find Java a more nimble, flexible and better supported language in general than ESQL.
|
Who said complex processing was required? Simple transforms may be all thats required.
Each to their own of course and I only said that it would not have been my chosen approach based on the requirements given. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Thu Sep 30, 2010 7:55 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
To understand this post, you may like to read the background:
http://en.wikipedia.org/wiki/Cavemen_%40TV_series%41
Quote: |
I'm interesting in knowing where the obsession with log4j came from, I've not seen this as a requirement frmo the OP.
|
The OP wants to send a text payload to a file. The natural extension of this desire is to send it to an auto-cleaning, self-rotating file SYSTEM. Who wants to be chained to a file system deleting old files all the time? We have the technology, let's rebuild him.
I beleive we can all get along. Even Cro-magnons and Sapiens. Now an eyebrow or three is raised when I started dating Kate. But since then, I let her go. Now everyone is giving Joel dirt about it. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Sep 30, 2010 7:59 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
If nothing else, I'd use getLastChild() to get the Message Body rather than all of those extra getNextSibling() calls.
The message body is guaranteed to be the last child of root.
or one could evaluate an Xpath to get Root.BLOB.BLOB. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Thu Sep 30, 2010 8:03 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
mqjeff wrote: |
If nothing else, I'd use getLastChild() to get the Message Body rather than all of those extra getNextSibling() calls.
The message body is guaranteed to be the last child of root.
or one could evaluate an Xpath to get Root.BLOB.BLOB. |
Yes, this is syntactically correct and more efficient.
However, I was verbose for training purposes. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
mqjeff |
Posted: Thu Sep 30, 2010 8:40 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
There's a fine line between "being verbose for training purposes" and "instilling bad habits".
I do not say which side of the line this one sits on, however.
In particular, it moves around with different students, and one can't tell from here much about afielden.
Who is presumably rather annoyed at all of this babbling to little end in their post on a straight forward topic. |
|
Back to top |
|
 |
afielden |
Posted: Fri Oct 01, 2010 6:58 am Post subject: |
|
|
Novice
Joined: 30 Sep 2010 Posts: 10
|
Interesting discussion.
I changed the FileInput parser domain to BLOB, and using the code suggested by lancelotlinc (thanks), I found that the second call to getNextSibling() of the root element returns null. However, after commenting this out, the eventual textOut value I get is "" (see code)
My problem here is, I don't know the structure of the MbElement tree, so I'm groping in the dark a bit. Does anyone know the correct route to navigate to my BLOB data?
Thanks.
Code: |
MbMessage message = assembly.getMessage();
// Walk the cursor down the tree to find the data.
MbElement cursor = null;
cursor = message.getRootElement(); // Root element.
cursor = cursor.getFirstChild(); // Properties element.
cursor = cursor.getNextSibling(); // MQMD element.
//cursor = cursor.getNextSibling(); // MQMD.Blob element. <- null
cursor = cursor.getFirstChild(); // Parser element.
cursor = cursor.getNextSibling(); // Parser.Blob element.
byte[] textIn = ((byte[]) cursor.getValue());
String textOut = new String(textIn);
|
|
|
Back to top |
|
 |
lancelotlinc |
Posted: Fri Oct 01, 2010 7:11 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
Sorry - I should have been more clear. The source example provided is for an MQInput node. Someone else will have to address a FileInput node. _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
zpat |
Posted: Fri Oct 01, 2010 7:24 am Post subject: |
|
|
 Jedi Council
Joined: 19 May 2001 Posts: 5866 Location: UK
|
You might have to add a cast to ensure the string is the correct codepage
Quote: |
SET OutputRoot.BLOB.BLOB = CAST(InputRoot.BLOB.BLOB AS CHAR CCSID InputRoot.MQMD.CodedCharSetId); |
|
|
Back to top |
|
 |
|