Package org.springframework.xd.dirt.stream

Source Code of org.springframework.xd.dirt.stream.JobDeployer

/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package org.springframework.xd.dirt.stream;

import static org.springframework.xd.dirt.stream.ParsingContext.job;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.MessageChannel;
import org.springframework.util.Assert;
import org.springframework.xd.dirt.integration.bus.MessageBus;
import org.springframework.xd.dirt.zookeeper.Paths;
import org.springframework.xd.dirt.zookeeper.ZooKeeperConnection;
import org.springframework.xd.module.ModuleDescriptor;

/**
* @author Glenn Renfro
* @author Luke Taylor
* @author Ilayaperumal Gopinathan
* @author Gunnar Hillert
*/
public class JobDeployer extends AbstractInstancePersistingDeployer<JobDefinition, Job> implements DisposableBean {

  private final String JOB_CHANNEL_PREFIX = "job:";

  private final MessageBus messageBus;

  private final ConcurrentMap<String, MessageChannel> jobChannels = new ConcurrentHashMap<String, MessageChannel>();

  public JobDeployer(ZooKeeperConnection zkConnection, JobDefinitionRepository definitionRepository,
      JobRepository instanceRepository,
      XDParser parser, MessageBus messageBus) {
    super(zkConnection, definitionRepository, instanceRepository, parser, job);
    Assert.notNull(messageBus, "MessageBus must not be null");
    this.messageBus = messageBus;
  }

  @Override
  protected Job makeInstance(JobDefinition definition) {
    return new Job(definition);
  }

  public void launch(String name, String jobParameters) {
    MessageChannel channel = jobChannels.get(name);
    if (channel == null) {
      jobChannels.putIfAbsent(name, new DirectChannel());
      channel = jobChannels.get(name);
      messageBus.bindProducer(JOB_CHANNEL_PREFIX + name, channel, null);
    }
    // Double check so that user gets an informative error message
    JobDefinition job = getDefinitionRepository().findOne(name);
    if (job == null) {
      throwNoSuchDefinitionException(name);
    }
    if (instanceRepository.findOne(name) == null) {
      throwNotDeployedException(name);
    }
    // todo: is this Assert necessary? if not we can remove the parser dependency and parse method
    Assert.isTrue(parse(name, job.getDefinition()).size() == 1, "Expecting only a single module");
    channel.send(MessageBuilder.withPayload(jobParameters != null ? jobParameters : "").build());
  }

  @Override
  protected JobDefinition createDefinition(String name, String definition) {
    return new JobDefinition(name, definition);
  }

  @Override
  protected String getDeploymentPath(JobDefinition definition) {
    return Paths.build(Paths.JOB_DEPLOYMENTS, definition.getName());
  }

  private List<ModuleDescriptor> parse(String name, String definition) {
    return parser.parse(name, definition, definitionKind);
  }

  @Override
  public void destroy() throws Exception {
    for (Map.Entry<String, MessageChannel> entry : jobChannels.entrySet()) {
      messageBus.unbindProducer(JOB_CHANNEL_PREFIX + entry.getKey(), entry.getValue());
    }
  }

}
TOP

Related Classes of org.springframework.xd.dirt.stream.JobDeployer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.