Search in sources :

Example 1 with ManagedLifecycle

use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.

the class CloneMediator method init.

public void init(SynapseEnvironment se) {
    synapseEnv = se;
    for (Target target : targets) {
        ManagedLifecycle seq = target.getSequence();
        if (seq != null) {
            seq.init(se);
        } else if (target.getSequenceRef() != null) {
            SequenceMediator targetSequence = (SequenceMediator) se.getSynapseConfiguration().getSequence(target.getSequenceRef());
            if (targetSequence == null || targetSequence.isDynamic()) {
                se.addUnavailableArtifactRef(target.getSequenceRef());
            }
        }
        Endpoint endpoint = target.getEndpoint();
        if (endpoint != null) {
            endpoint.init(se);
        }
    }
}
Also used : Target(org.apache.synapse.mediators.eip.Target) Endpoint(org.apache.synapse.endpoints.Endpoint) SequenceMediator(org.apache.synapse.mediators.base.SequenceMediator) ManagedLifecycle(org.apache.synapse.ManagedLifecycle)

Example 2 with ManagedLifecycle

use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.

the class QuartzTaskManager method schedule.

@Override
public boolean schedule(TaskDescription taskDescription) {
    assertInitialized();
    assertStarted();
    if (taskDescription == null) {
        throw new SynapseTaskException("Task Description cannot be found", logger);
    }
    Trigger trigger;
    JobDetail jobDetail;
    synchronized (lock) {
        if (triggerFactory == null) {
            throw new SynapseTaskException("TriggerFactory cannot be found", logger);
        }
        if (jobDetailFactory == null) {
            throw new SynapseTaskException("JobDetailFactory cannot be found", logger);
        }
        trigger = triggerFactory.createTrigger(taskDescription);
        if (trigger == null) {
            throw new SynapseTaskException("Trigger cannot be created from : " + taskDescription, logger);
        }
        jobDetail = jobDetailFactory.createJobDetail(taskDescription, taskDescription.getResources(), SimpleQuartzJob.class);
        if (jobDetail == null) {
            throw new SynapseTaskException("JobDetail cannot be created from : " + taskDescription + " and job class " + taskDescription.getTaskImplClassName(), logger);
        }
    }
    Object clsInstance = taskDescription.getResource(TaskDescription.INSTANCE);
    if (clsInstance == null) {
        String className = (String) taskDescription.getProperty(TaskDescription.CLASSNAME);
        try {
            clsInstance = Class.forName(className).newInstance();
            if (clsInstance instanceof ManagedLifecycle) {
                Object se = properties.get(TaskConstants.SYNAPSE_ENV);
                if (!(se instanceof SynapseEnvironment)) {
                    return false;
                }
                ((ManagedLifecycle) clsInstance).init((SynapseEnvironment) se);
            }
            for (Object property : taskDescription.getXmlProperties()) {
                OMElement prop = (OMElement) property;
                logger.debug("Found Property : " + prop.toString());
                PropertyHelper.setStaticProperty(prop, clsInstance);
            }
        } catch (ClassNotFoundException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        } catch (InstantiationException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        } catch (IllegalAccessException e) {
            logger.error("Could not schedule task[" + name + "].", e);
            return false;
        }
    }
    if (!(clsInstance instanceof Task)) {
        logger.error("Could not schedule task[" + name + "]. Cannot load class " + "org.apache.synapse.startup.quartz.SimpleQuartzJob");
        return false;
    }
    jobDetail.getJobDataMap().put(TaskDescription.INSTANCE, clsInstance);
    jobDetail.getJobDataMap().put(TaskDescription.CLASSNAME, clsInstance.getClass().toString());
    jobDetail.getJobDataMap().put(TaskConstants.SYNAPSE_ENV, getProperty(TaskConstants.SYNAPSE_ENV));
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("scheduling job : " + jobDetail + " with trigger " + trigger);
        }
        if (taskDescription.getCount() != 0 && !isTaskRunning(jobDetail.getKey())) {
            try {
                synchronized (lock) {
                    scheduler.scheduleJob(jobDetail, trigger);
                }
            } catch (ObjectAlreadyExistsException e) {
                logger.warn("did not schedule the job : " + jobDetail + ". the job is already running.");
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("did not schedule the job : " + jobDetail + ". count is zero.");
            }
        }
    } catch (SchedulerException e) {
        throw new SynapseTaskException("Error scheduling job : " + jobDetail + " with trigger " + trigger);
    }
    logger.info("Scheduled task [" + taskDescription.getName() + "::" + taskDescription.getTaskGroup() + "]");
    return true;
}
Also used : Task(org.apache.synapse.task.Task) SchedulerException(org.quartz.SchedulerException) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) SynapseTaskException(org.apache.synapse.task.SynapseTaskException) ManagedLifecycle(org.apache.synapse.ManagedLifecycle) JobDetail(org.quartz.JobDetail) Trigger(org.quartz.Trigger) ObjectAlreadyExistsException(org.quartz.ObjectAlreadyExistsException)

Example 3 with ManagedLifecycle

use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.

the class SimpleQuartzJob method execute.

public void execute(JobExecutionContext ctx) throws JobExecutionException {
    String jobName = ctx.getJobDetail().getKey().getName();
    if (log.isDebugEnabled()) {
        log.debug("Executing task : " + jobName);
    }
    JobDataMap jdm = ctx.getMergedJobDataMap();
    String jobClassName = (String) jdm.get(CLASSNAME);
    if (jobClassName == null) {
        handleException("No " + CLASSNAME + " in JobDetails");
    }
    boolean initRequired = false;
    Task task = (Task) jdm.get(TaskDescription.INSTANCE);
    if (task == null) {
        initRequired = true;
    }
    SynapseEnvironment se = (SynapseEnvironment) jdm.get("SynapseEnvironment");
    if (initRequired) {
        try {
            task = (Task) getClass().getClassLoader().loadClass(jobClassName).newInstance();
        } catch (Exception e) {
            handleException("Cannot instantiate task : " + jobClassName, e);
        }
        Set properties = (Set) jdm.get(PROPERTIES);
        for (Object property : properties) {
            OMElement prop = (OMElement) property;
            log.debug("Found Property : " + prop.toString());
            PropertyHelper.setStaticProperty(prop, task);
        }
        // 1. Initialize
        if (task instanceof ManagedLifecycle && se != null) {
            ((ManagedLifecycle) task).init(se);
        }
    }
    // 2. Execute
    if (se != null && task != null && se.isInitialized()) {
        task.execute();
    }
    if (initRequired) {
        // 3. Destroy
        if (task instanceof ManagedLifecycle && se != null) {
            ((ManagedLifecycle) task).destroy();
        }
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Task(org.apache.synapse.task.Task) Set(java.util.Set) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) OMElement(org.apache.axiom.om.OMElement) ManagedLifecycle(org.apache.synapse.ManagedLifecycle) JobExecutionException(org.quartz.JobExecutionException)

Example 4 with ManagedLifecycle

use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.

the class AbstractListMediator method init.

/**
 * Initialize child mediators recursively
 * @param se synapse environment
 */
public void init(SynapseEnvironment se) {
    if (log.isDebugEnabled()) {
        log.debug("Initializing child mediators of mediator : " + getType());
    }
    for (int i = 0; i < mediators.size(); i++) {
        Mediator mediator = mediators.get(i);
        mediator.setMediatorPosition(i);
        if (mediator instanceof ManagedLifecycle) {
            ((ManagedLifecycle) mediator).init(se);
        }
        if (mediator.isContentAware()) {
            if (log.isDebugEnabled()) {
                log.debug(mediator.getType() + " is content aware, setting sequence <" + getType() + "> as content aware");
            }
            sequenceContentAware = true;
        }
    }
}
Also used : Mediator(org.apache.synapse.Mediator) ManagedLifecycle(org.apache.synapse.ManagedLifecycle)

Example 5 with ManagedLifecycle

use of org.apache.synapse.ManagedLifecycle in project wso2-synapse by wso2.

the class IndirectEndpoint method reLoadAndInitEndpoint.

/**
 * Reload as needed , either from registry , local entries or predefined endpoints
 * @param cc ConfigurationContext
 */
private synchronized void reLoadAndInitEndpoint(ConfigurationContext cc) {
    Parameter parameter = cc.getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_CONFIG);
    Parameter synEnvParameter = cc.getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV);
    if (parameter.getValue() instanceof SynapseConfiguration && synEnvParameter.getValue() instanceof SynapseEnvironment) {
        SynapseConfiguration synCfg = (SynapseConfiguration) parameter.getValue();
        SynapseEnvironment synapseEnvironment = (SynapseEnvironment) synEnvParameter.getValue();
        boolean reLoad = (realEndpoint == null);
        if (!reLoad) {
            Entry entry = synCfg.getEntryDefinition(key);
            if (entry != null && entry.isDynamic()) {
                if (!entry.isCached() || entry.isExpired()) {
                    reLoad = true;
                }
            } else {
                // If the endpoint is static we should reload it from the Synapse config
                reLoad = true;
            }
        }
        if (reLoad) {
            if (log.isDebugEnabled()) {
                log.debug("Loading real endpoint with key : " + key);
            }
            realEndpoint = synCfg.getEndpoint(key);
            if (realEndpoint != null && !realEndpoint.isInitialized()) {
                realEndpoint.init(synapseEnvironment);
            }
        } else {
            Endpoint epr = synCfg.getEndpoint(key);
            if (epr != realEndpoint) {
                realEndpoint = epr;
                if (realEndpoint != null && !realEndpoint.isInitialized() && realEndpoint instanceof ManagedLifecycle) {
                    realEndpoint.init(synapseEnvironment);
                }
            }
        }
    }
}
Also used : Entry(org.apache.synapse.config.Entry) SynapseEnvironment(org.apache.synapse.core.SynapseEnvironment) Axis2SynapseEnvironment(org.apache.synapse.core.axis2.Axis2SynapseEnvironment) Parameter(org.apache.axis2.description.Parameter) SynapseConfiguration(org.apache.synapse.config.SynapseConfiguration) ManagedLifecycle(org.apache.synapse.ManagedLifecycle)

Aggregations

ManagedLifecycle (org.apache.synapse.ManagedLifecycle)14 Endpoint (org.apache.synapse.endpoints.Endpoint)6 Axis2SynapseEnvironment (org.apache.synapse.core.axis2.Axis2SynapseEnvironment)4 SequenceMediator (org.apache.synapse.mediators.base.SequenceMediator)4 Mediator (org.apache.synapse.Mediator)3 SynapseConfiguration (org.apache.synapse.config.SynapseConfiguration)3 SynapseEnvironment (org.apache.synapse.core.SynapseEnvironment)3 Properties (java.util.Properties)2 OMElement (org.apache.axiom.om.OMElement)2 TestMessageContext (org.apache.synapse.TestMessageContext)2 PriorityExecutor (org.apache.synapse.commons.executors.PriorityExecutor)2 ProxyService (org.apache.synapse.core.axis2.ProxyService)2 InboundEndpoint (org.apache.synapse.inbound.InboundEndpoint)2 Target (org.apache.synapse.mediators.eip.Target)2 TemplateMediator (org.apache.synapse.mediators.template.TemplateMediator)2 MessageProcessor (org.apache.synapse.message.processor.MessageProcessor)2 AbstractMessageProcessor (org.apache.synapse.message.processor.impl.AbstractMessageProcessor)2 MessageStore (org.apache.synapse.message.store.MessageStore)2 API (org.apache.synapse.rest.API)2 Task (org.apache.synapse.task.Task)2