Example usage for org.springframework.statemachine StateContext getExtendedState

List of usage examples for org.springframework.statemachine StateContext getExtendedState

Introduction

In this page you can find the example usage for org.springframework.statemachine StateContext getExtendedState.

Prototype

ExtendedState getExtendedState();

Source Link

Document

Gets the state machine extended state.

Usage

From source file:org.springframework.cloud.deployer.spi.yarn.YarnAppDeployer.java

@Override
public String deploy(AppDeploymentRequest request) {
    logger.info("Deploy request for {}", request);
    final AppDefinition definition = request.getDefinition();
    Map<String, String> definitionParameters = definition.getProperties();
    Map<String, String> deploymentProperties = request.getDeploymentProperties();
    logger.info("Deploying request for definition {}", definition);
    logger.info("Parameters for definition {}", definitionParameters);
    logger.info("Deployment properties for request {}", deploymentProperties);

    int count = 1;
    String countString = request.getDeploymentProperties().get(AppDeployer.COUNT_PROPERTY_KEY);
    if (StringUtils.hasText(countString)) {
        count = Integer.parseInt(countString);
    }/*from   w w w  .j  a v  a 2s . c o  m*/
    final String group = request.getDeploymentProperties().get(AppDeployer.GROUP_PROPERTY_KEY);
    Resource resource = request.getResource();
    final String clusterId = group + ":" + definition.getName();

    // contextRunArgs are passed to boot app ran to control yarn apps
    ArrayList<String> contextRunArgs = new ArrayList<String>();
    contextRunArgs.add("--spring.yarn.appName=scdstream:app:" + group);

    // deployment properties override servers.yml which overrides application.yml
    for (Entry<String, String> entry : deploymentProperties.entrySet()) {
        if (entry.getKey().startsWith("spring.cloud.deployer.yarn.app.streamappmaster")) {
            contextRunArgs.add("--" + entry.getKey() + "=" + entry.getValue());
        } else if (entry.getKey().startsWith("spring.cloud.deployer.yarn.app.streamcontainer")) {
            // weird format with '--' is just straight pass to appmaster
            contextRunArgs.add("--spring.yarn.client.launchcontext.arguments.--" + entry.getKey() + "='"
                    + entry.getValue() + "'");
        }
    }

    String baseDir = yarnDeployerProperties.getBaseDir();
    if (!baseDir.endsWith("/")) {
        baseDir = baseDir + "/";
    }

    String artifactPath = isHdfsResource(resource) ? getHdfsArtifactPath(resource)
            : baseDir + "/artifacts/cache/";
    contextRunArgs
            .add("--spring.yarn.client.launchcontext.arguments.--spring.cloud.deployer.yarn.appmaster.artifact="
                    + artifactPath);

    // TODO: using default app name "app" until we start to customise
    //       via deploymentProperties
    final Message<String> message = MessageBuilder.withPayload(AppDeployerStateMachine.EVENT_DEPLOY)
            .setHeader(AppDeployerStateMachine.HEADER_APP_VERSION, "app")
            .setHeader(AppDeployerStateMachine.HEADER_CLUSTER_ID, clusterId)
            .setHeader(AppDeployerStateMachine.HEADER_GROUP_ID, group)
            .setHeader(AppDeployerStateMachine.HEADER_COUNT, count)
            .setHeader(AppDeployerStateMachine.HEADER_ARTIFACT, resource)
            .setHeader(AppDeployerStateMachine.HEADER_ARTIFACT_DIR, artifactPath)
            .setHeader(AppDeployerStateMachine.HEADER_DEFINITION_PARAMETERS, definitionParameters)
            .setHeader(AppDeployerStateMachine.HEADER_CONTEXT_RUN_ARGS, contextRunArgs).build();

    // Use of future here is to set id when it becomes available from machine
    final SettableListenableFuture<String> id = new SettableListenableFuture<>();
    final StateMachineListener<String, String> listener = new StateMachineListenerAdapter<String, String>() {

        @Override
        public void stateContext(StateContext<String, String> stateContext) {
            if (stateContext.getStage() == Stage.STATE_ENTRY
                    && stateContext.getTarget().getId().equals(AppDeployerStateMachine.STATE_READY)) {
                if (ObjectUtils.nullSafeEquals(message.getHeaders().getId().toString(), stateContext
                        .getExtendedState().get(AppDeployerStateMachine.VAR_MESSAGE_ID, String.class))) {
                    Exception exception = stateContext.getExtendedState().get(AppDeployerStateMachine.VAR_ERROR,
                            Exception.class);
                    if (exception != null) {
                        id.setException(exception);
                    } else {
                        String applicationId = stateContext.getStateMachine().getExtendedState()
                                .get(AppDeployerStateMachine.VAR_APPLICATION_ID, String.class);
                        DeploymentKey key = new DeploymentKey(group, definition.getName(), applicationId);
                        id.set(key.toString());
                    }
                }
            }
        }
    };
    stateMachine.addStateListener(listener);
    id.addCallback(new ListenableFutureCallback<String>() {

        @Override
        public void onSuccess(String result) {
            stateMachine.removeStateListener(listener);
        }

        @Override
        public void onFailure(Throwable ex) {
            stateMachine.removeStateListener(listener);
        }
    });

    stateMachine.sendEvent(message);
    // we need to block here until SPI supports
    // returning id asynchronously
    try {
        return id.get(2, TimeUnit.MINUTES);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.springframework.cloud.deployer.spi.yarn.YarnTaskLauncher.java

@Override
public String launch(AppDeploymentRequest request) {
    logger.info("Deploy request for {}", request);
    logger.info("Deploy request deployment properties {}", request.getDeploymentProperties());
    logger.info("Deploy definition {}", request.getDefinition());
    Resource resource = request.getResource();
    AppDefinition definition = request.getDefinition();

    String artifact = resource.getFilename();

    final String name = definition.getName();
    Map<String, String> definitionParameters = definition.getProperties();
    Map<String, String> deploymentProperties = request.getDeploymentProperties();
    List<String> commandlineArguments = request.getCommandlineArguments();
    String appName = "scdtask:" + name;

    // contextRunArgs are passed to boot app ran to control yarn apps
    // we pass needed args to control module coordinates and params,
    // weird format with '--' is just straight pass to container
    ArrayList<String> contextRunArgs = new ArrayList<String>();

    contextRunArgs.add("--spring.yarn.appName=" + appName);
    for (Entry<String, String> entry : definitionParameters.entrySet()) {
        if (StringUtils.hasText(entry.getValue())) {
            contextRunArgs.add(//  w w w.ja v a 2 s  . co  m
                    "--spring.yarn.client.launchcontext.arguments.--spring.cloud.deployer.yarn.appmaster.parameters."
                            + entry.getKey() + ".='" + entry.getValue() + "'");
        }
    }

    int index = 0;
    for (String commandlineArgument : commandlineArguments) {
        contextRunArgs.add("--spring.yarn.client.launchcontext.argumentsList[" + index
                + "]='--spring.cloud.deployer.yarn.appmaster.commandlineArguments[" + index + "]="
                + commandlineArgument + "'");
        index++;
    }

    String baseDir = yarnDeployerProperties.getBaseDir();
    if (!baseDir.endsWith("/")) {
        baseDir = baseDir + "/";
    }
    String artifactPath = isHdfsResource(resource) ? getHdfsArtifactPath(resource)
            : baseDir + "/artifacts/cache/";

    contextRunArgs.add(
            "--spring.yarn.client.launchcontext.arguments.--spring.yarn.appmaster.launchcontext.archiveFile="
                    + artifact);
    contextRunArgs
            .add("--spring.yarn.client.launchcontext.arguments.--spring.cloud.deployer.yarn.appmaster.artifact="
                    + artifactPath + artifact);

    // deployment properties override servers.yml which overrides application.yml
    for (Entry<String, String> entry : deploymentProperties.entrySet()) {
        if (StringUtils.hasText(entry.getValue())) {
            if (entry.getKey().startsWith("spring.cloud.deployer.yarn.app.taskcontainer")) {
                contextRunArgs.add("--spring.yarn.client.launchcontext.arguments.--" + entry.getKey() + "='"
                        + entry.getValue() + "'");
            } else if (entry.getKey().startsWith("spring.cloud.deployer.yarn.app.taskappmaster")) {
                contextRunArgs.add("--" + entry.getKey() + "=" + entry.getValue());
            }
        }
    }

    final Message<String> message = MessageBuilder.withPayload(TaskLauncherStateMachine.EVENT_LAUNCH)
            .setHeader(TaskLauncherStateMachine.HEADER_APP_VERSION, "app")
            .setHeader(TaskLauncherStateMachine.HEADER_ARTIFACT, resource)
            .setHeader(TaskLauncherStateMachine.HEADER_ARTIFACT_DIR, artifactPath)
            .setHeader(TaskLauncherStateMachine.HEADER_DEFINITION_PARAMETERS, definitionParameters)
            .setHeader(TaskLauncherStateMachine.HEADER_CONTEXT_RUN_ARGS, contextRunArgs).build();

    // setup future, listen event from machine and finally unregister listener,
    // and set future value
    final SettableListenableFuture<String> id = new SettableListenableFuture<>();
    final StateMachineListener<String, String> listener = new StateMachineListenerAdapter<String, String>() {

        @Override
        public void stateContext(StateContext<String, String> stateContext) {
            if (stateContext.getStage() == Stage.STATE_ENTRY
                    && stateContext.getTarget().getId().equals(TaskLauncherStateMachine.STATE_READY)) {
                if (ObjectUtils.nullSafeEquals(message.getHeaders().getId().toString(), stateContext
                        .getExtendedState().get(TaskLauncherStateMachine.VAR_MESSAGE_ID, String.class))) {
                    Exception exception = stateContext.getExtendedState()
                            .get(TaskLauncherStateMachine.VAR_ERROR, Exception.class);
                    if (exception != null) {
                        id.setException(exception);
                    } else {
                        String applicationId = stateContext.getStateMachine().getExtendedState()
                                .get(TaskLauncherStateMachine.VAR_APPLICATION_ID, String.class);
                        DeploymentKey key = new DeploymentKey(name, applicationId);
                        id.set(key.toString());
                    }
                }
            }
        }
    };

    stateMachine.addStateListener(listener);
    id.addCallback(new ListenableFutureCallback<String>() {

        @Override
        public void onSuccess(String result) {
            stateMachine.removeStateListener(listener);
        }

        @Override
        public void onFailure(Throwable ex) {
            stateMachine.removeStateListener(listener);
        }
    });

    stateMachine.sendEvent(message);
    // we need to block here until SPI supports
    // returning id asynchronously
    try {
        return id.get(2, TimeUnit.MINUTES);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * Tasks choice guard. This {@link Guard} will check if related
 * extended state variables contains negative values for related
 * tasks id's and returns true if so, else false.
 *
 * @return the guard//from  w  ww .j a va  2  s .  c o  m
 */
private Guard<String, String> tasksChoiceGuard() {
    return new Guard<String, String>() {

        @Override
        public boolean evaluate(StateContext<String, String> context) {
            Map<Object, Object> variables = context.getExtendedState().getVariables();
            for (Entry<Object, Object> entry : variables.entrySet()) {
                if (entry.getKey() instanceof String
                        && ((String) entry.getKey()).startsWith(STATE_TASKS_PREFIX)) {
                    if (entry.getValue() instanceof Integer) {
                        Integer value = (Integer) entry.getValue();
                        if (value < 0) {
                            if (log.isDebugEnabled()) {
                                log.debug("Task id=[" + entry.getKey()
                                        + "] has negative execution value, tasksChoiceGuard returns true");
                            }
                            listener.onTasksError();
                            return true;
                        }
                    }
                }
            }
            listener.onTasksSuccess();
            return false;
        }
    };
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * {@link Action} calls {@link TasksListener#onTasksAutomaticFix(StateContext)}
 * before checking status of extended state variables related to tasks. If all
 * variables are ok, event {@code EVENT_CONTINUE} is sent, otherwise event
 * {@code EVENT_FALLBACK} is send which takes state machine into a manual handling.
 *
 * @return the action/* w w  w. j av a 2s.c  o m*/
 */
private Action<String, String> automaticAction() {
    return new Action<String, String>() {

        @Override
        public void execute(StateContext<String, String> context) {

            listener.onTasksAutomaticFix(TasksHandler.this, context);

            boolean hasErrors = false;
            Map<Object, Object> variables = context.getExtendedState().getVariables();
            for (Entry<Object, Object> entry : variables.entrySet()) {
                if (entry.getKey() instanceof String
                        && ((String) entry.getKey()).startsWith(STATE_TASKS_PREFIX)) {
                    if (entry.getValue() instanceof Integer) {
                        Integer value = (Integer) entry.getValue();
                        if (value < 0) {
                            hasErrors = true;
                            break;
                        }
                    }
                }
            }
            if (hasErrors) {
                context.getStateMachine().sendEvent(EVENT_FALLBACK);
            } else {
                context.getStateMachine().sendEvent(EVENT_CONTINUE);
            }
        }
    };
}

From source file:org.springframework.statemachine.recipes.tasks.TasksHandler.java

/**
 * {@link Action} which resets related extended state variables
 * to zero for tasks order to indicate a fixed tasks.
 *
 * @return the action// w  w w .j ava2  s  .c  o m
 */
private Action<String, String> fixAction() {
    return new Action<String, String>() {

        @Override
        public void execute(StateContext<String, String> context) {
            Map<Object, Object> variables = context.getExtendedState().getVariables();
            for (Entry<Object, Object> entry : variables.entrySet()) {
                if (entry.getKey() instanceof String
                        && ((String) entry.getKey()).startsWith(STATE_TASKS_PREFIX)) {
                    if (entry.getValue() instanceof Integer) {
                        Integer value = (Integer) entry.getValue();
                        if (value < 0) {
                            variables.put(entry.getKey(), 0);
                        }
                    }
                }
            }
        }
    };
}