ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » javacompute node to build body of the message

Post new topic  Reply to topic
 javacompute node to build body of the message « View previous topic :: View next topic » 
Author Message
vennela
PostPosted: Wed Feb 01, 2006 1:11 pm    Post subject: javacompute node to build body of the message Reply with quote

Jedi Knight

Joined: 11 Aug 2002
Posts: 4055
Location: Hyderabad, India

I am trying to use JavaCompute node to build the body of my outgoing message.
For now I am trying to create the XML elements within the code. I am able to set the MQMD fields successfully. But the data is not getting in the output message. Below is the code I am using.

Let me know what the mistake is.

Code:
      MbOutputTerminal out = getOutputTerminal("out");
      MbOutputTerminal alt = getOutputTerminal("alternate");

      MbMessage inMessage = contact admin.getMessage();

      // create new message
      //MbMessage outMessage = new MbMessage(inMessage);
      MbMessage outMessage = new MbMessage();
      MbMessageAssembly outAssembly = new MbMessageAssembly(contact admin,
            outMessage);

      try {
         // ----------------------------------------------------------
         // Add user code below

         MbElement inputRoot = inMessage.getRootElement();
         MbElement inputBody = inputRoot.getLastChild();
         MbElement outputRoot = outMessage.getRootElement();
         //MbElement outputBody = outputRoot.getLastChild();
         
         MbElement mqmd = outputRoot.createElementAsFirstChild("MQHMD");
         mqmd.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,"Format","XML");
         mqmd.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "ReplyToQ","ReplyToQ1");
         
         
                  
         MbElement outBody = outputRoot.getLastChild();
         MbElement outName = outBody.createElementAsLastChild(MbElement.TYPE_NAME, "Name", null);
         outName.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "FirstName","John");         
         outName.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "LastName","Smith");   
         // End of user code
         // ----------------------------------------------------------

         // The following should only be changed
         // if not propagating message to the 'out' terminal
         out.propagate(outAssembly);



The output I am trying for is

Code:
<Name>
<FirstName>John </FirstName>
<LastName>Smith</LastName>
</Name>
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dipankar
PostPosted: Wed Feb 01, 2006 8:31 pm    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2005
Posts: 171

Try with this.

Code:

import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;
/*
 * Created on Jan 13, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author mqsiuid
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class CreateNewMsg_ComputeNode_JavaCompute extends MbJavaComputeNode {

   public void evaluate(MbMessageAssembly contact admin) throws MbException {
      MbOutputTerminal out = getOutputTerminal("out");
      MbOutputTerminal alt = getOutputTerminal("alternate");

      MbMessage inMessage = contact admin.getMessage();

      // create new message
      MbMessage outMessage = new MbMessage();
      MbMessageAssembly outAssembly = new MbMessageAssembly(contact admin,
            outMessage);

//       optionally copy message headers
      copyMessageHeaders(inMessage, outMessage);
      
//      MbElement outBody = outRoot.
      MbElement outRoot = outMessage.getRootElement().createElementAsLastChild("XML");
      
      MbElement Employee = outRoot.createElementAsFirstChild(MbElement.TYPE_NAME,"Name",null);
      MbElement EmpName = Employee.createElementAsLastChild(MbElement.TYPE_NAME,"FirstName","John");
      //MbElement EmpTitle = EmpName.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,"Title","Paul");
      MbElement EmpDsg = EmpName.createElementAfter(MbElement.TYPE_NAME,"LastName","Smith");
      
      

      // ----------------------------------------------------------
      // Add user code below

      // End of user code
      // ----------------------------------------------------------

      // The following should only be changed
      // if not propagating message to the 'out' terminal
      out.propagate(outAssembly);

      // clear the outMessage
      outMessage.clearMessage();
   }

   public void copyMessageHeaders(MbMessage inMessage, MbMessage outMessage)
         throws MbException {
      MbElement outRoot = outMessage.getRootElement();

      // iterate though the headers starting with the first child of the root element
      MbElement header = inMessage.getRootElement().getFirstChild();
      while (header != null && header.getNextSibling() != null) // stop before the last child (body)
      {
         // copy the header and add it to the out message
         outRoot.addAsLastChild(header.copy());
         // move along to next header
         header = header.getNextSibling();
      }
   }
}



I have just followed the following link

http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/index.jsp?topic=/com.ibm.etools.mft.doc/ac30350_.htm
_________________
Regards
Back to top
View user's profile Send private message
elvis_gn
PostPosted: Wed Feb 01, 2006 9:27 pm    Post subject: Reply with quote

Padawan

Joined: 08 Oct 2004
Posts: 1905
Location: Dubai

Hi vennela,

Both your's and dipankar's code looks same...

The one noticable difference was the below
vennela wrote:
MbElement outBody = outputRoot.getLastChild();

whereas dipankar's
dipankar wrote:
MbElement outRoot = outMessage.getRootElement().createElementAsLastChild("XML");

The name of the body is not set.

This could be the reason why it did not work.

Regards.
Back to top
View user's profile Send private message Send e-mail
vennela
PostPosted: Wed Feb 01, 2006 9:40 pm    Post subject: Reply with quote

Jedi Knight

Joined: 11 Aug 2002
Posts: 4055
Location: Hyderabad, India

I am pretty sure the above code also wouldn't work, though I did not try it myself.
I saw the link posted above and that is one of the pages I used to code.
IMHO, I don't think it is that straight forward.
We'll have to use MbXPath's extensions to set fields.

I had to make two modifications.
1. For the MQInput message, I had to set the domain as XML. I guess, this could be done inside the compute node by specifying the parser, but I haven't figured that yet.
2. Issue MbXpath commands to set values.
Here is the code snippet that is working.

I copied the input message to output message. Then I am setting the FirstName value here.

Code:


         MbElement inputRoot = inMessage.getRootElement();
         MbElement inputBody = inputRoot.getLastChild();
         MbElement outputRoot = outMessage.getRootElement();
         MbElement outputBody = outputRoot.getLastChild();
         
         MbElement newName = outputBody.getFirstChild();
         String sFN = "Siri";
         
         MbXPath setFN = new MbXPath("//FirstName[set-value($xsFN)]");
         setFN.assignVariable("xsFN", sFN);
         newName.evaluateXPath(setFN);
Back to top
View user's profile Send private message Send e-mail Visit poster's website
dipankar
PostPosted: Thu Feb 02, 2006 1:13 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2005
Posts: 171

Hi elvis_gn and vennela,
Have you tested my code???????
I am dead sure that my code will work as I have tested it in my system. And the result is same as vennela expected.

Code:
MbElement outRoot = outMessage.getRootElement().createElementAsLastChild("XML");


As far as I know, the above line is equivalent to OutputRoot.XML and the way to specify the domain.

See the following documents
Quote:
In the template code, the default constructor of MbMessage is called to create a blank message, as shown in the following Java code:
MbMessage outMessage = new MbMessage();
The headers can be copied from the incoming message using the supplied utility method, copyMessageHeaders(), as shown in this Java code:
copyMessageHeaders(inMessage, outMessage);
The new message body can now be created. First, add the top level parser element. For XML, this is:
MbElement outRoot = outMessage.getRootElement();
MbElement outBody = outRoot.createElementAsLastChild("XMLNSC");
The remainder of the message can then be built up using the createElement methods and the extended syntax of the broker XPath implementation.


Here I have used 'XML' instead of 'XMLNSC'

Besides this, I have used TYPE_NAME instead of TYPE_NAME_VALUE.
TYPE_NAME_VALUE is used for creating attribute.
_________________
Regards
Back to top
View user's profile Send private message
dipankar
PostPosted: Thu Feb 02, 2006 1:36 am    Post subject: Reply with quote

Disciple

Joined: 03 Feb 2005
Posts: 171

Vennela,

Quote:
We'll have to use MbXPath's extensions to set fields.


This is not only one option to create element for outbound message.

Please have a look at the following link
http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/index.jsp?topic=/com.ibm.etools.mft.doc/ac30340_.htm

Quote:
I copied the input message to output message


I believe that for new output message, we need only to copy message header as we do in ESQL, not the full message. It would have been inconvenient.

Please correct me if I am wrong.
_________________
Regards
Back to top
View user's profile Send private message
elvis_gn
PostPosted: Thu Feb 02, 2006 1:51 am    Post subject: Reply with quote

Padawan

Joined: 08 Oct 2004
Posts: 1905
Location: Dubai

Hi dipankar,

dipankar wrote:
I believe that for new output message, we need only to copy message header as we do in ESQL, not the full message.

You are right, but I guess vennela did it only for his testing purposes.

vennela:
I think you should try removing the Xpath and only setting the domain...lets make sure that we have no option but to use Xpath.

Regards.
Back to top
View user's profile Send private message Send e-mail
vennela
PostPosted: Thu Feb 02, 2006 9:06 am    Post subject: Reply with quote

Jedi Knight

Joined: 11 Aug 2002
Posts: 4055
Location: Hyderabad, India

Yes, the above code posted by dipankar worked.
Thank you.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » javacompute node to build body of the message
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.