3
I have a controller annotated with @Scheduled
but when the test environment is active it also runs.
It is possible to check the environment in which application is running so that the method is run only in production?
3
I have a controller annotated with @Scheduled
but when the test environment is active it also runs.
It is possible to check the environment in which application is running so that the method is run only in production?
3
You can create a variable in a test property file to have its test properties separate from "default" (from the file application.properties
)
First, we will create the file in the following format, to create a profile (profile) test (tst
):
application-tst.properties
Included in this properties file a configuration for you to understand which environment you are in:
ambiente = teste
And in the original archive application.properties
, add:
ambiente = producao
So we distinguish the two environments. Now you can run your application using this new profile (tst
).
mvn spring-boot:run -Drun.profiles=tst
Or:
java -jar -Dspring.profiles.active=tst XXX.jar
Then use the annotation @ConditionalOnProperty
:
@ConditionalOnProperty(name="ambiente", havingValue="producao")
public class Schedule {
@Scheduled
public class algumScheduler() {
// código
}
}
If you prefer, you can use the @ConditionalOnProperty
in the method itself also, if you wish.
Thank you for helping me, and this scenario would work but with the table of parameters for the system (as the suggestion above) I had more facility to resolve the issue.
@Joycesd, legal, simple solutions are almost always the best.
2
If there is any table of parameters for the system, you could add the information of the environment and check if it is of production to be able to execute the method. Depending on your scenario this would be a simpler solution.
Browser other questions tagged java spring spring-boot
You are not signed in. Login or sign up in order to post.
Does this test environment go up using another Spring properties file? Your code needs to know somehow that you are in a different environment (in this case, testing) to be able to control this.
– Dherik
@Dherik is the same file. Is there any way to separate them?
– Joyce SD
I believe that this here help you, specifically the second part of his response, when he uses
@ConditionalOnProperty
.– StatelessDev