Wednesday, 18 June 2014

Accessing Configurable Service using Java

Configurable Services are typically run time 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.

1. Userdefined Configurable Service
2. Java Method
3. ESQL

Create a user defined configurable service
Go to Websphere MQ > Configurable Service > New > Configurable Service
Select type as Userdefined configurable service.
Add property in the GUI.
Or
You can use command to create Configurable service with Key and Value
mqsicreateconfigurableservice IB9NODE -c UserDefined -o UD1 -n
BHANU -v "2032" 

Java Class
To retrieve the Value from the configurable service we use a simple java call.
We need a ConfigurationManagerProxy jar file in our project workspace.

Version 1 :
package com.get.configvalue;
import com.ibm.broker.config.proxy.*;
public class GET_CONFIG_VALUE
{
public static String getValue(String strKey)
{
String strValue = null;
try{
BrokerProxy b = BrokerProxy.getLocalInstance();

while(!b.hasBeenPopulatedByBroker())
 {
}

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

strValue =CS_set[0].getProperties().getProperty(strKey);
}
catch (Exception e)
{
e.printStackTrace();
}
return strValue;
}
}


Version 2 :
package com.external.java;
import com.ibm.broker.config.proxy.*;
public class GetConfigProperty
{
public static String getValue(String ConfigService,String KeyValue)
{
String ResultValue = null;
try{
BrokerProxy b = BrokerProxy.getLocalInstance();

while(!b.hasBeenPopulatedByBroker()) 
{
}

ConfigurableService CS_set =b.getConfigurableService("UserDefined",ConfigService);

ResultValue = CS_set.getProperties().getProperty(KeyValue);
}
catch (Exception e)
{
e.printStackTrace();
}
return ResultValue;
}
}

ESQL

Version 1 :
Calling Java method in ESQL

CREATE PROCEDURE getValueFromConfig (IN KeyChar CHAR)
RETURNS CHAR
LANGUAGE JAVA
EXTERNAL NAME "com.get.configvalue.GET_CONFIG_VALUE.getValue";

getValueFromConfig : Procedure Name
KeyChar : Input Parameter
com.get.configvalue : Package Name
GET_CONFIG_VALUE : Java Class Name
getValue : Java Method

Calling ESQL procedure

DECLARE strKey CHARACTER 'BHANU';
--result will be stored in strValue

DECLARE strValue CHARACTER;
--retrieves the Value from configurable service properties

CALL getValueFromConfig(strKey) INTO strValue;
 -- (Key:BHANU Value : 2032)

CALL getValueFromConfig('SampleKey') INTO strValue;
 --(Key:SampleKey Value:SampleValue)

Version 2 :
Calling Java method in ESQL

CREATE PROCEDURE getValueFromConfig
(IN ConfigService CHARACTER,IN KeyValue CHARACTER)
RETURNS CHAR
LANGUAGE JAVA
EXTERNAL NAME "com.external.java.GetConfigProperty.getValue";

getValueFromConfig : Procedure Name
ConfigService : ConfigService Name
KeyValue : Key Value in Properties file
GetConfigProperty : Java Class Name
getValue : Java Method

Calling ESQL procedure

DECLARE strKey CHARACTER 'BHANU';
--result will be stored in strValue

DECLARE strValue CHARACTER;
--retrieves the Value from configurable service properties

CALL getValueFromConfig('UD1','BHANU') INTO strValue; 
--(Key:BHANU Value : 2032)

CALL getValueFromConfig('UD2','UD2') INTO strValue;
CALL getValueFromConfig('AD1','AD1') INTO strValue;