How to pass parameter to java lamda

2561 views java

I am using Spring RetryTemplate and using this method. Wanted to pass some argument (vendor) it is giving me compilation error. I can create a another variable vendorName as final can send it. But I want to make use the the variable vendor. It must be simple one but not getting it. please help.

public Token getToken(final String tokenId) {
    String vendor = getVendor(tokenId);//returns some vendor name
    RetryTemplate retryTemplate = getRetryTemplate();
    Token token = retryTemplate.execute(context -> {"Attempted {} times", context.getRetryCount());
        return retrieveToken(tokenId, vendor);

private RetryTemplate getRetryTemplate() {

    final FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
    fixedBackOffPolicy.setBackOffPeriod(getRandomNumber() * 1000);

    final SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();

    final RetryTemplate retryTemplate = new RetryTemplate();

    return retryTemplate;

compilation error is: Local variable vendor defined in an enclosing scope must be final or effectively final

answered question

So, what's the exact and comple compilation error?

I updated with error. It is related java lambda passing parameter.

@Kiran vendor seems to be effectively final from what i can observe

That code can't produce that error.

1 Answer


You cannot use non-final variables in a lambda.

Set vendor to final

posted this

Have an answer?


Please login first before posting an answer.