Monday, September 19, 2016

Disable the Task Execution in ESB Cluster Environment


All of us know, one of the key feature of ESB cluster setup is deployment synchronizing. Once you deploy a project in the cluster manager mode, it will sync up with the cluster worker nodes. However, eventually the artifacts will be deployed in manager and worker nodes.

Suppose if we deploy the artifacts having "Task" in the cluster environment, they will be deployed on all the ESB nodes in cluster including manager node. But the task is something like a schedule job. So it will automatically execute in the deployed nodes without any invocation. Simply it will execute in the manager node as well as worker nodes in ESB 4.8.1.

But the issue is basically we are expecting the manager node just do the deployment synchronizing activities and not the service execution. At this point of view, ESB allows us to deploy the tasks in cluster environment and allow the execution of them in predefined nodes. This attribute is called as pinnedServers attribute.
We can specify the pinned server attribute in the task synapse configuration and that will get executed only on that specific pinned servers.

 <?xml version="1.0" encoding="UTF-8"?>  
 <task xmlns="http://ws.apache.org/ns/synapse" name="Task123" class="org.apache.synapse.startup.tasks.MessageInjector" group="synapse.simple.quartz" pinnedServers="worker1, worker2">  
  </task>  

The worker nodes should be configured as the pinned serversWe just need to add the <ServerName> configuration in axis2.xml in each worker node which required to execute that task and start the server with -DSynapseServerName=<ServerName>

  <!-- Synapse Server name parameter -->  
   <parameter name="SynapseConfig.ServerName" locked="false">worker1</parameter>  

Monday, September 12, 2016

Accessing registry property value using class mediator


How to retrieve the properties in Synapse 


The get-property() function allows any XPath expression used in a configuration to look up information from the current message context. Using the Property mediator, we can retrieve properties for different scopes. You can find the different scopes to retrieve properties here.

How to use get-property() to access the property saved in registry.


To retrieve the prorerties saved in registry, we need to use the scope "registry".The property name should be appended to the property path using @ sign.

 get-property('registry', String registryPath@propertyName)  

How to access the property saved in registry using class mediator.


Step 1


We need to write the following logic in the class mediator. In the below sample we have saved the property name as "lastAccessTime" in the registry path  "gov:/tasks/EventAlertScheduledTask".


 package Mypackage;  
 import org.apache.synapse.MessageContext;   
 import org.apache.synapse.mediators.AbstractMediator;  
 import org.apache.synapse.registry.Registry;  
 import org.apache.synapse.config.Entry;  
 import java.util.Properties;  
 public class Sample extends AbstractMediator {   
      public boolean mediate(MessageContext synCtx) {   
           // TODO Implement your mediation logic here  
           String[] regParam = "gov:/tasks/EventAlertScheduledTask@lastAccessTime".split("@");  
            String regPath = regParam[0];  
      String propName = regParam[1];  
     Entry propEntry = synCtx.getConfiguration().getEntryDefinition(regPath);  
     Registry registry = synCtx.getConfiguration().getRegistry();  
     if (registry != null) {  
       registry.getResource(propEntry, new Properties());  
       if (propName != null) {  
         Properties reqProperties = propEntry.getEntryProperties();  
         if (reqProperties != null) {  
           if (reqProperties.get(propName) != null) {  
             System.out.println("Property value is : "+reqProperties.getProperty(propName));  
           }  
         }  
       }  
      }  
     return true;  
 }  
 }  


Step 2


The following proxy service can be used to invoke the above class mediator.

 <?xml version="1.0" encoding="UTF-8"?>  
 <proxy xmlns="http://ws.apache.org/ns/synapse"  
     name="TestProxy"  
     transports="https,http"  
     statistics="disable"  
     trace="disable"  
     startOnLoad="true">  
   <target>  
    <inSequence>  
      <property name="property_value"  
           expression="get-property('registry','gov:/tasks/EventAlertScheduledTask@lastAccessTime')"/>  
      <log>  
       <property name="Access using property mediator :"  
            expression="get-property('property_value')"/>  
      </log>  
      <class name="Mypackage.Sample"/>  
      <drop/>  
    </inSequence>  
   </target>  
   <description/>  
 </proxy>