0

The application.yml file has a Long type setting:

producer.init-delay: 15000

It is used in this method:

@Scheduled(fixedRate = 25000, initialDelay = "#{new Long('${producer.init-delay}')}")
public produce() {}

Take an example from the question:Spring @Value TypeMismatchException:...

But I have this code gives an error:

Incompatible types. Found: 'java.lang.String', required: 'long'
  • On a sidenote: the c'tor `Long(long)` is deprecated. You may want to use the factory method. See [this question](https://stackoverflow.com/questions/49904830/create-a-new-integer-object-that-holds-the-value-1/) for details (Disclaimer: I wrote the accepted answer to this question). – Turing85 Mar 20 '21 at 09:33
  • this will help https://stackoverflow.com/a/63146218/9050514 – deadshot Mar 20 '21 at 09:38
  • You can use @Value for the field but you can't use this field in your annotation parameter, because the annotation parameter should be set in compilation time duration. – Dmitrii B Mar 20 '21 at 09:41
  • To the question: I am not able to find the relevant JLS, but the values of an annotation must be compile-time constants. Thus if `initialDelay` is defined as `int`, it must be assigned a value of type `int` that is known at compile-time. – Turing85 Mar 20 '21 at 09:47

1 Answers1

1

According to the javadoc of the @Scheduled, initialDelay must be of type long and you have it as String.

Every long argument in @Scheduled has a string alternative. Use them:

@Scheduled(fixedRate = 25000, initialDelayString = "${producer.init-delay}")
public produce() {}
Dmitrii Bocharov
  • 872
  • 6
  • 21
  • I apologize for the inaccurately worded question. I need to understand how to get the Long number from properties. I'll probably have to ask a new question. –  Mar 20 '21 at 10:37
  • @alexSerg but this is how you can get it. The code I showed in the answer will do it. If you want the Long itself, then use `@Value("${producer.init-delay}")`. It will do what you want – Dmitrii Bocharov Mar 20 '21 at 13:44
  • This does not answer the question... – breakline Nov 16 '22 at 08:16