Showing posts with label Java Compute Node. Show all posts
Showing posts with label Java Compute Node. Show all posts

(WMB) Printing XML in JavaCompute

Printing application data in xml format in Java compute node is bit tricky, If we simply print the body (payload) we will not have data in xml format. We have to make use of toBitstream method to convert the body into an array of bytes & then print.

Excerpt of evaluate method :

  MbMessage message = assembly.getMessage();
MbElement root = message.getRootElement();
MbElement body = root.getLastChild();
byte[] b = null;
b = body.toBitstream("", "", "", 0, 0, 0);
 // This example is taken with XMLNSC as parser for input so no need to specify messageType, messageSet & wire format. 
 //For MRM parser, these parameters has to be specified.

try {
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(b);
InputStreamReader inputStreamReader = new InputStreamReader(arrayInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
char[] c = new char[b.length];
bufferedReader.read(c, 0, c.length);
System.out.println(c);
} catch (Exception e) {
e.printStackTrace();
}

Signature of toBitstream method :
                  
public byte[] toBitstream(String messageType, String messageSet, String messageFormat,int encoding,int ccsid, int options) throws MbException

(WMB) Accessing Configurable Service using Java Compute in WMB V7.0


Configurable Services are typically runtime properties. We can use them to define properties that are related to external services on which the broker relies. Instead of defining properties on the node or message flow, we can create configurable services so that nodes and message flows can refer to them to find properties at run time. If we use this method, we can change the values of attributes for a configurable service on the broker, which then affects the behavior of a node or message flow without the need for redeployment.


This post shows how to use the CMP API in a JavaCompute node to query, set, create, and delete properties dynamically at run time in configurable services that we have defined with type UserDefined.


Java code :
Add the CMP JAR file install_dir/classes/ConfigManagerProxy.jar to the Java build path for the associated Java project.
Following imports are required for code :
  • import com.ibm.broker.javacompute.MbJavaComputeNode;
  • import com.ibm.broker.plugin.*;
  • import com.ibm.broker.config.proxy.*;


Main Code 


try{

BrokerProxy b = BrokerProxy.getLocalInstance();

//To ensure that the BrokerProxy object has been populated with data from the broker before we access the configurable service, Add the following code:
                
while(!b.hasBeenPopulatedByBroker()) {} 
                
//As we have defined the configurable service with type 'UserDefined'; Add following code:

ConfigurableService[] CS_set = b.getConfigurableServices("UserDefined"); 

System.out.println("Configurable Service Name :"+CS_set[0].getName()); 

System.out.println("Property [Prop1] : "+CS_set[0].getProperties().getProperty("Prop1"));

CS_set[0].setProperty("Prop2" , "Setting property"); //To set a property

CS_set[0].deleteProperty("Prop2"); //To delete a property

}
catch (Exception e)
{
e.printStackTrace();
}


Output :
Configurable Service Name :UD1
Property [Prop1]: Prop1_Value

Setting Configurable Service in MQ explorer

Some useful commands :

  • mqsireportproperties ECSBRK -c UserDefined -o AllReportableEntityNames -r //To report Configurable Service of type 'UserDefined '.
  • mqsicreateconfigurableservice ECSBRK -c UserDefined -o UD2 -n Prop3 -v Value3 //To create a Configurable Service.
  • mqsideleteconfigurableservice ECSBRK -c UserDefined -o UD2 //To delete
  • mqsichangeproperties ECSBRK -c UserDefined -o UD2 -n Prop4 -v Value4  // To add another property under same Configurable Service name UD2
  • mqsichangeproperties ECSBRK -c UserDefined -o UD2 -n Prop3 -v Value33 // To change the existing property value.