Example usage for org.springframework.util Assert state

List of usage examples for org.springframework.util Assert state

Introduction

In this page you can find the example usage for org.springframework.util Assert state.

Prototype

public static void state(boolean expression, Supplier<String> messageSupplier) 

Source Link

Document

Assert a boolean expression, throwing an IllegalStateException if the expression evaluates to false .

Usage

From source file:org.springframework.batch.core.step.AbstractStep.java

@Override
public void afterPropertiesSet() throws Exception {
    Assert.state(name != null, "A Step must have a name");
    Assert.state(jobRepository != null, "JobRepository is mandatory");
}

From source file:org.springframework.batch.core.step.item.SimpleStepFactoryBean.java

/**
 * @param step//from   w  ww.j  av  a 2  s .  c om
 * 
 */
protected void applyConfiguration(TaskletStep step) {

    Assert.state(getItemReader() != null, "ItemReader must be provided");
    Assert.state(getItemWriter() != null || getItemProcessor() != null,
            "ItemWriter or ItemProcessor must be provided");
    Assert.state(transactionManager != null, "TransactionManager must be provided");

    step.setTransactionManager(transactionManager);
    step.setTransactionAttribute(getTransactionAttribute());
    step.setJobRepository(jobRepository);
    step.setStartLimit(startLimit);
    step.setAllowStartIfComplete(allowStartIfComplete);

    registerStreams(step, streams);

    if (chunkOperations == null) {
        RepeatTemplate repeatTemplate = new RepeatTemplate();
        repeatTemplate.setCompletionPolicy(getChunkCompletionPolicy());
        chunkOperations = repeatTemplate;
    }

    if (stepOperations == null) {

        stepOperations = new RepeatTemplate();

        if (taskExecutor != null) {
            TaskExecutorRepeatTemplate repeatTemplate = new TaskExecutorRepeatTemplate();
            repeatTemplate.setTaskExecutor(taskExecutor);
            repeatTemplate.setThrottleLimit(throttleLimit);
            stepOperations = repeatTemplate;
        }

        ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);

    }

    step.setStepOperations(stepOperations);

    SimpleChunkProvider<T> chunkProvider = configureChunkProvider();

    SimpleChunkProcessor<T, S> chunkProcessor = configureChunkProcessor();

    registerItemListeners(chunkProvider, chunkProcessor);
    registerStepListeners(step, chunkOperations);
    registerStreams(step, itemReader, itemProcessor, itemWriter);

    ChunkOrientedTasklet<T> tasklet = new ChunkOrientedTasklet<T>(chunkProvider, chunkProcessor);
    tasklet.setBuffering(!isReaderTransactionalQueue());

    step.setTasklet(tasklet);

}

From source file:org.springframework.batch.core.step.item.SimpleStepFactoryBean.java

/**
 * @return a {@link CompletionPolicy} consistent with the commit interval
 * and injected policy (if present)./*from   w w  w .  ja v  a  2 s.  c  o m*/
 */
private CompletionPolicy getChunkCompletionPolicy() {
    Assert.state(!(chunkCompletionPolicy != null && commitInterval != 0),
            "You must specify either a chunkCompletionPolicy or a commitInterval but not both.");
    Assert.state(commitInterval >= 0, "The commitInterval must be positive or zero (for default value).");

    if (chunkCompletionPolicy != null) {
        return chunkCompletionPolicy;
    }
    if (commitInterval == 0) {
        logger.info("Setting commit interval to default value (" + DEFAULT_COMMIT_INTERVAL + ")");
        commitInterval = DEFAULT_COMMIT_INTERVAL;
    }
    return new SimpleCompletionPolicy(commitInterval);
}

From source file:org.springframework.batch.core.step.tasklet.TaskletStep.java

@Override
public void afterPropertiesSet() throws Exception {
    super.afterPropertiesSet();
    Assert.state(transactionManager != null, "A transaction manager must be provided");
}

From source file:org.springframework.batch.integration.chunk.ChunkMessageChannelItemWriter.java

/**
 * Get the next result if it is available (within the timeout specified in the gateway), otherwise do nothing.
 *
 * @throws AsynchronousFailureException If there is a response and it contains a failed chunk response.
 *
 * @throws IllegalStateException if the result contains the wrong job instance id (maybe we are sharing a channel
 * and we shouldn't be)/*w  w w . j a v a2 s  . c o  m*/
 */
private void getNextResult() throws AsynchronousFailureException {
    Message<ChunkResponse> message = (Message<ChunkResponse>) messagingGateway.receive(replyChannel);
    if (message != null) {
        ChunkResponse payload = message.getPayload();
        if (logger.isDebugEnabled()) {
            logger.debug("Found result: " + payload);
        }
        Long jobInstanceId = payload.getJobId();
        Assert.state(jobInstanceId != null, "Message did not contain job instance id.");
        Assert.state(jobInstanceId.equals(localState.getJobId()), "Message contained wrong job instance id ["
                + jobInstanceId + "] should have been [" + localState.getJobId() + "].");
        if (payload.isRedelivered()) {
            logger.warn(
                    "Redelivered result detected, which may indicate stale state. In the best case, we just picked up a timed out message "
                            + "from a previous failed execution. In the worst case (and if this is not a restart), "
                            + "the step may now timeout.  In that case if you believe that all messages "
                            + "from workers have been sent, the business state "
                            + "is probably inconsistent, and the step will fail.");
            localState.incrementRedelivered();
        }
        localState.pushResponse(payload);
        localState.incrementActual();
        if (!payload.isSuccessful()) {
            throw new AsynchronousFailureException(
                    "Failure or interrupt detected in handler: " + payload.getMessage());
        }
    }
}

From source file:org.springframework.batch.integration.chunk.RemoteChunkHandlerFactoryBean.java

/**
 * Builds a {@link ChunkHandler} from the {@link ChunkProcessor} extracted from the {@link #setStep(TaskletStep)
 * step} provided. Also modifies the step to send chunks to the chunk handler via the
 * {@link #setChunkWriter(ItemWriter) chunk writer}.
 * //from ww  w . ja v  a  2 s.c  om
 * @see FactoryBean#getObject()
 */
public ChunkHandler<T> getObject() throws Exception {

    if (stepContributionSource == null) {
        Assert.state(chunkWriter instanceof StepContributionSource,
                "The chunk writer must be a StepContributionSource or else the source must be provided explicitly");
        stepContributionSource = (StepContributionSource) chunkWriter;
    }

    Assert.state(step instanceof TaskletStep, "Step [" + step.getName() + "] must be a TaskletStep");
    if (logger.isDebugEnabled()) {
        logger.debug("Converting TaskletStep with name=" + step.getName());
    }

    Tasklet tasklet = getTasklet((TaskletStep) step);
    Assert.state(tasklet instanceof ChunkOrientedTasklet<?>,
            "Tasklet must be ChunkOrientedTasklet in step=" + step.getName());

    ChunkProcessor<T> chunkProcessor = getChunkProcessor((ChunkOrientedTasklet<?>) tasklet);
    Assert.state(chunkProcessor != null,
            "ChunkProcessor must be accessible in Tasklet in step=" + step.getName());

    ItemWriter<T> itemWriter = getItemWriter(chunkProcessor);
    Assert.state(!(itemWriter instanceof ChunkMessageChannelItemWriter<?>),
            "Cannot adapt step [" + step.getName()
                    + "] because it already has a remote chunk writer.  Use a local writer in the step.");

    replaceChunkProcessor((ChunkOrientedTasklet<?>) tasklet, chunkWriter, stepContributionSource);
    if (chunkWriter instanceof StepExecutionListener) {
        step.registerStepExecutionListener((StepExecutionListener) chunkWriter);
    }

    ChunkProcessorChunkHandler<T> handler = new ChunkProcessorChunkHandler<T>();
    setNonBuffering(chunkProcessor);
    handler.setChunkProcessor(chunkProcessor);
    // TODO: create step context for the processor in case it has
    // scope="step" dependencies
    handler.afterPropertiesSet();

    return handler;

}

From source file:org.springframework.batch.item.data.AbstractNeo4jItemReader.java

/**
 * Checks mandatory properties/*from www . ja v a2 s . c  om*/
 *
 * @see InitializingBean#afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() throws Exception {
    Assert.state(template != null || sessionFactory != null,
            "A Neo4JOperations implementation or SessionFactory is required");
    Assert.state(targetType != null, "The type to be returned is required");
    Assert.state(StringUtils.hasText(startStatement), "A START statement is required");
    Assert.state(StringUtils.hasText(returnStatement), "A RETURN statement is required");
    Assert.state(StringUtils.hasText(orderByStatement), "A ORDER BY statement is required");
}

From source file:org.springframework.batch.item.data.Neo4jItemWriter.java

/**
 * Checks mandatory properties/*from w ww  .  j  ava 2s . co m*/
 *
 * @see InitializingBean#afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() throws Exception {
    Assert.state(template != null || this.sessionFactory != null,
            "A Neo4JOperations implementation or a SessionFactory is required");

    this.useSession = this.sessionFactory != null;
}

From source file:org.springframework.batch.item.data.RepositoryItemReader.java

@Override
public void afterPropertiesSet() throws Exception {
    Assert.state(repository != null, "A PagingAndSortingRepository is required");
    Assert.state(pageSize > 0, "Page size must be greater than 0");
    Assert.state(sort != null, "A sort is required");
}

From source file:org.springframework.batch.item.data.RepositoryItemWriter.java

/**
 * Check mandatory properties - there must be a repository.
 *//* www  . ja va  2s .  co m*/
@Override
public void afterPropertiesSet() throws Exception {
    Assert.state(repository != null, "A CrudRepository implementation is required");
}