Invoking WSO2 ESB Proxy Service from Scheduled Task


Following java class invoke the given proxy (proxy name should be given as the parameter) and the inbuilt message body is send to the specified proxy service.

NOTE: Following sample is scheduled task implementation class in WSO2 ESB.

package com.test.task;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.description.AxisService;
import org.apache.axis2.engine.AxisEngine;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.SynapseException;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.core.axis2.Axis2SynapseEnvironment;
import org.apache.synapse.task.Task;
import org.apache.synapse.util.PayloadHelper;

/**
 * @author isuru
 * 
 */
public class RuleInvokerTask implements Task, ManagedLifecycle {

	/**
	 * Logger.
	 */
	private static final Log LOG = LogFactory.getLog(RuleInvokerTask.class);

	/**
	 * Synapse Environment which is passed in init() method.
	 */
	private SynapseEnvironment synapseEnvironment;

	/**
	 * ProxyName to be invoked.
	 */
	private String proxyName;

	@Override
	public void init(SynapseEnvironment synapseEnvironment) {
		this.synapseEnvironment = synapseEnvironment;
	}

	@Override
	public void execute() {

		LOG.info("Executing Rule Invoker Task...");

		if (this.synapseEnvironment == null) {
			String msg = "Synapse Environment is not set";
			LOG.error(msg);
			throw new SynapseException(msg);
		}

		org.apache.axis2.context.MessageContext axis2MsgCtx = new org.apache.axis2.context.MessageContext();

		ConfigurationContext configurationContext = ((Axis2SynapseEnvironment) this.synapseEnvironment)
				.getAxis2ConfigurationContext();

		axis2MsgCtx.setConfigurationContext(configurationContext);
		axis2MsgCtx.setIncomingTransportName("local");
		axis2MsgCtx.setServerSide(true);
		axis2MsgCtx.setSoapAction("urn:mediate");

		try {
			AxisService axisService = configurationContext
					.getAxisConfiguration().getService(this.proxyName);
			if (axisService == null) {
				throw new SynapseException("Proxy Service: " + this.proxyName
						+ " not found");
			}
			axis2MsgCtx.setAxisService(axisService);

		} catch (AxisFault axisFault) {
			throw new SynapseException(
					"Error occurred while attempting to find the Proxy Service");
		}

		SOAPEnvelope envelope = null;
		envelope = OMAbstractFactory.getSOAP12Factory().createSOAPEnvelope();
		invokeProxy(axis2MsgCtx, envelope);

	}

	@Override
	public void destroy() {
	}

	/**
	 * Invoke proxy.
	 *
	 * @param axis2MsgCtx
	 *            axis2 Message Context.
	 * @param envelope
	 *            Soap Envelope.
	 */
	private void invokeProxy(MessageContext axis2MsgCtx, SOAPEnvelope envelope) {

		OMElement ruleIdElement = generateBody();

		try {
			PayloadHelper.setXMLPayload(envelope,
					ruleIdElement.cloneOMElement());
			axis2MsgCtx.setEnvelope(envelope);

		} catch (AxisFault axisFault) {
			throw new SynapseException(
					"Error in setting the message payload : " + axisFault);
		}

		try {
			AxisEngine.receive(axis2MsgCtx);

		} catch (AxisFault axisFault) {
			throw new SynapseException(
					"Error occurred while invoking proxy service : "
							+ this.proxyName);
		}
	}

	/**
	 * This method generated the {@link OMElement} which goes under the soapbody
	 * element. The element is as follows,
	 *
         * @return body.
         */
         private OMElement generateBody() {

         if (LOG.isDebugEnabled()) {
         LOG.debug("Generating soap body content");
         }

         OMFactory factory = OMAbstractFactory.getOMFactory();
         OMNamespace ns = factory.createOMNamespace("http://bimaas.com", "bim");
         OMElement rule = factory.createOMElement("rule", ns);

         // Setting projectId
         OMElement projectIdElement = factory.createOMElement("projectId", ns);
         OMText projectIdText = factory.createOMText(projectIdElement,"7777");
         projectIdElement.addChild(projectIdText);

         // Setting ruleId
         OMElement ruleIdElement = factory.createOMElement("ruleId", ns);
         OMText ruleIdText = factory.createOMText(ruleIdElement,"99");
         ruleIdElement.addChild(ruleIdText);

         rule.addChild(projectIdElement);
         rule.addChild(ruleIdElement);

         return rule;
         }

         /**
         * @return the proxyName
         */
         public final String getProxyName() {
         return proxyName;
         }

         /**
         * @param proxyName
         * the proxyName to set
         */
         public final void setProxyName(String proxyName) {
         this.proxyName = proxyName;
         }

}