Quotes

Wednesday, July 11, 2018

JCN




The UserDefined configurable service is only accessible via a Java compute node. It cannot be accessed directly in ESQL. If you want to use ESQL, you need to create the code in Java and then use a function call to the Java code.
To set up the ESQL function call:
  1. CREATE PROCEDURE getUserDefinedConfigServProp( IN P1 CHARACTER )
  2. LANGUAGE JAVA
  3. EXTERNAL NAME "com.ibm.broker.test.MyClass.getUserDefinedConfigServProp";
To call this procuedure:
  1. DECLARE result CHARACTER getUserDefinedConfigServProp('test');
And the Java code could look something like this:
  1. public static String getUserDefinedConfigServProp(String cs){
  2. String rs = "exception occurred: ";
  3. try{
  4. BrokerProxy bp = BrokerProxy.getLocalInstance();
  5. short c = 0;
  6. while(!bp.hasBeenPopulatedByBroker()){
  7. try{
  8. c++; //loop count, in the rare case it fails, we need to leave the loop
  9. Thread.sleep(100);
  10. }catch(InterruptedException e){
  11. return rs+e.toString();
  12. }
  13. if(c > 4)
  14. return rs+"timed out";
  15. }
  16. try{
  17. ConfigurableService udcs = bp.getConfigurableService("UserDefined", cs);
  18. Properties props = udcs.getProperties();
  19. String out = "";
  20. for(String key : props.stringPropertyNames()){
  21. out = out+key+"="+props.getProperty(key)+",";
  22. }
  23. if(out.length > 0)
  24. rs=out.substring(0,out.length()-1);
  25. else
  26. rs=rs+"no properties returned"
  27. }catch(ConfigManagerProxyPropertyNotInitializedException e){
  28. return rs+e.toString();
  29. }
  30. }catch(ConfigManagerProxyLoggedException e){
  31. return rs+e.toString();
  32. }
  33. return rs;
  34. }


package bpmfacade.subflows;

import java.util.HashMap;
import java.util.Properties;

import com.ibm.broker.config.proxy.BrokerProxy;
import com.ibm.broker.config.proxy.ConfigManagerProxyLoggedException;
import com.ibm.broker.config.proxy.ConfigManagerProxyPropertyNotInitializedException;
import com.ibm.broker.config.proxy.ConfigurableService;
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.MbElement;
import com.ibm.broker.plugin.MbException;
import com.ibm.broker.plugin.MbMessage;
import com.ibm.broker.plugin.MbMessageAssembly;
import com.ibm.broker.plugin.MbOutputTerminal;
import com.ibm.broker.plugin.MbUserException;

public class ReadConfiguration extends MbJavaComputeNode {

public  HashMap<String,String> getUserDefinedConfigServProp(String[] keys){
      String rs = "exception occurred: ";
      BrokerProxy bp =null;
      HashMap<String,String> properties = new HashMap<String,String>();
      boolean cacheExists =false;
      try{
      for (String key : keys) {
      if((rs = CacheManager.readFromCache("BPMConfigCache", key)) != null)
      {
      cacheExists = true;
      properties.put(key, rs);
      }
      }
      if(cacheExists == false)
      {
      // If it does not exist in the cache get it from config service
          bp = BrokerProxy.getLocalInstance();
          while(!bp.hasBeenPopulatedByBroker()) { Thread.sleep(100); }
      for (String key : keys) {
               // Check cache
      if((rs = CacheManager.readFromCache("BPMConfigCache", key)) != null)
      {
      properties.put(key, rs);
      }
      else
      {
           try{
                ConfigurableService udcs = bp.getConfigurableService("UserDefined",
                "BPMFacadeConfiguration");
                Properties props = udcs.getProperties();
                String out = "";
                out = props.getProperty(key);
                if(out == "")
                {
                     rs=rs + "no property returned";
                }
                else
                {
                rs = out;
                }
    // Insert into Cache
    CacheManager.insertIntoCache("BPMConfigCache", key, rs);
           }
           catch(ConfigManagerProxyPropertyNotInitializedException e){
                //return rs+e.toString();
           }
           properties.put(key, rs);
      }
    }
      }
      }
      catch(ConfigManagerProxyLoggedException e){
           //return rs+e.toString();
      } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
      finally{
      if(bp != null)
      {
      bp.disconnect();
      }
      }
      return properties;
}


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

MbMessage inMessage = inAssembly.getMessage();
MbMessageAssembly outAssembly = null;
try {
// create new message as a copy of the input
MbMessage outMessage = new MbMessage(inMessage);
outAssembly = new MbMessageAssembly(inAssembly, outMessage);
// ----------------------------------------------------------
// Add user code below
String[] keys = {"Process_Server_URL", "User_Identity","User_Password", "Max_Users"};
HashMap<String,String> properties =  getUserDefinedConfigServProp(keys);

String process_Server_URL = properties.get("Process_Server_URL");
String user_Identity = properties.get("User_Identity");
String user_Password = properties.get("User_Password");
String max_Users = properties.get("Max_Users");



MbMessage env = inAssembly.getGlobalEnvironment();
MbMessage lenv = inAssembly.getLocalEnvironment();
//MbMessage newEnv = new MbMessage(env);

env.getRootElement().createElementAsFirstChild(
MbElement.TYPE_NAME_VALUE,
"Process_Server_URL",
process_Server_URL);
env.getRootElement().createElementAsFirstChild(
MbElement.TYPE_NAME_VALUE,
"User_Identity",
user_Identity);
env.getRootElement().createElementAsFirstChild(
MbElement.TYPE_NAME_VALUE,
"User_Password",
user_Password);
env.getRootElement().createElementAsFirstChild(
MbElement.TYPE_NAME_VALUE,
"Max_Users",
max_Users);

outAssembly = new MbMessageAssembly(
inAssembly,
lenv,
inAssembly.getExceptionList(),
inAssembly.getMessage());

// End of user code
// ----------------------------------------------------------
} catch (MbException e) {
// Re-throw to allow Broker handling of MbException
throw e;
} catch (RuntimeException e) {
// Re-throw to allow Broker handling of RuntimeException
throw e;
} catch (Exception e) {
// Consider replacing Exception with type(s) thrown by user code
// Example handling ensures all exceptions are re-thrown to be handled in the flow
throw new MbUserException(this, "evaluate()", "", "", e.toString(),
null);
}
// The following should only be changed
// if not propagating message to the 'out' terminal
out.propagate(outAssembly);

}

}

1 comment:

  1. hello Vardhan, you mentioned getConfigurableService method, but forgot call getConfigurableServices, it allows to renew props from broker. https://www.ibm.com/support/knowledgecenter/SSMKHH_10.0.0/com.ibm.etools.mft.cmp.doc/com/ibm/broker/config/proxy/BrokerProxy.html

    ReplyDelete