After deploying spring boot project on jboss ,every things work great except my schdule task.i have annotated my main class with #enablescheduling and #component on my schedule method class and #scheduled on my method.but after running successfuly without error on jboss ,my schedule task does not fire.i really need a solution.
Tnx every body
Below is my config class and my schedule classes:
#SpringBootApplicatio
#Enablescheduling
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
#component
Public class test{
#scheduled(fixedDelay = 1000)
Public void print(){
//do sth
}
}
#SpringBootApplicatio
#Enablescheduling
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class);
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
#component
Public class test{
#scheduled(fixeddelay.....)
Public void print(){
//do sth
}
}
Ok, we've found an issue. Setting initialDelay to 10000 ms resolved the problem in our case. Don't know an explanation but still: it works.
Related
I am working on a Spring Batch project that has two Main classes with #SpringBootApplication in it. I also have to use the #Scheduler and associate it only with one Main class. But the issue here is no matter which class I run, the scheduler is getting executed. Snippets below,
MainApp1.java
#SpringBootApplication
#EnableScheduling
public class MainApp1{
public static void main(String[] args) {
SpringApplication.run(MainApp1.class, args);
}
}
MainApp2.java
#SpringBootApplication
public class MainApp2{
public static void main(String[] args) {
SpringApplication.run(MainApp2.class, args);
}
}
Scheduler.java
#Configuration
public class TmsBatchSenderScheduler {
#Scheduled(fixedDelay = 5000)
public void myScheduler() {
}
}
I think what's happening here is the scheduler is getting created for both mains because of the #Configuration annotation. Is there a way to achieve this? I want the scheduler to run only when the MainApp1 is run.
Thanks in advance :)
schedule task not working after deploying spring boot project in jboss, every thing works well except schedule tasks
#SpringBootApplication
#EnableScheduling
public class Application extends SpringBootServletInitializer{
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception
SpringApplication.run(Application.class);
}
}
//this is schedule task class in diffrent package
#Component
public class ScheduledTasks {
scheduled(fixedDelay = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateTest.format(new Date()));
}
What is the right way for creating test data upon server startup and inserting them into the database (I'm using a JPA/JDBC backed Postgres instance).
Preferably in form of creating Entities and having them persisted through a Repository interface rather than writing plain SQL code. Something like RoR's Rake db:seed helper.
If the framework exposes a hook for doing stuff when all the beans have been injected and the database is ready, that could also work.
You can catch ApplicationReadyEvent then insert demo data, for example:
#Component
public class DemoData {
#Autowired
private final EntityRepository repo;
#EventListener
public void appReady(ApplicationReadyEvent event) {
repo.save(new Entity(...));
}
}
Or you can implement CommandLineRunner or ApplicationRunner, to load demo data when an application is fully started:
#Component
public class DemoData implements CommandLineRunner {
#Autowired
private final EntityRepository repo;
#Override
public void run(String...args) throws Exception {
repo.save(new Entity(...));
}
}
#Component
public class DemoData implements ApplicationRunner {
#Autowired
private final EntityRepository repo;
#Override
public void run(ApplicationArguments args) throws Exception {
repo.save(new Entity(...));
}
}
Or even implement them like a Bean right in your Application (or other 'config') class:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Bean
public CommandLineRunner demoData(EntityRepository repo) {
return args -> {
repo.save(new Entity(...));
}
}
}
From Spring documentation: http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/#howto-database-initialization
Initialize a database using Hibernate
A file named import.sql in the root of the classpath will be executed on startup if Hibernate creates the schema from scratch (that is if the ddl-auto property is set to create or create-drop). This can be useful for demos and for testing if you are careful, but probably not something you want to be on the classpath in production. It is a Hibernate feature (nothing to do with Spring).
You can do like this
#SpringBootApplication
public class H2Application {
public static void main(String[] args) {
SpringApplication.run(H2Application.class, args);
}
#Bean
CommandLineRunner init (StudentRepo studentRepo){
return args -> {
List<String> names = Arrays.asList("udara", "sampath");
names.forEach(name -> studentRepo.save(new Student(name)));
};
}
}
I'm deploying my SpringBoot Application to a Tomcat 8.5 container.
Similar as described here: http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html I modified my SpringBootApplication, so it's deployable as war.
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(Application.class);
}
}
Similiar to this question Activating Spring #Profile Using JNDI I wan't the application to select the active Profile using an JNDI entry.
I added an EnvironmentApplicationContextInitializer identical to the one shown in the accepted answer.
However: I do not use any web.xml. So the profile is not being picked up and used.
How do I make the SpringBootApplication using the EnvironmentApplicationContextInitializer?
Thanks to M Deinum's comment I found a solution:
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
// this would be used if run via java -jar service.war
// SpringApplication app = new SpringApplication(Application.class);
// CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
// app.addInitializers(initializer);
// app.run(args);
SpringApplication.run(Application.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// this will be used within an app container
CustomApplicationContextInitializer initializer = new CustomApplicationContextInitializer();
return builder.initializers(initializer).sources(Application.class);
}
}
Note the builder.initializers(initializer) part.
How to set active profile in spring boot Application. This application will be deployed in stand alone Tomcat.
I have 2 property files application-{profile}.properties.
My Application class
#SpringBootApplication
public class Application extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
if I run the app with embedded tomcat the dev profile is set as active and it works fine. But when I deploy in stand alone tomcat. It does not work.
I tried to set active profile in configure method. but i get null pointer exception, when i get the environment from the context.
Any help on how to set the active profile.
You can set additional profiles on start up:
SpringApplication springApp = new SpringApplication(Main.class);
springApp.setAdditionalProfiles("profile1", "profile2");
springApp.run(args);
I also had the same problem and after struggling for half a day I ended up with this:
#SpringBootApplication
public class MyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
SpringApplication.run(MyApplication.class, args);
}
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "dev");
super.onStartup(servletContext);
}
}
Another way of doing it in Spring Boot 2 is by using SpringApplicationBuilder:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
#SpringBootApplication
public class MySpringProgram {
public static void main(String[] args) {
new SpringApplicationBuilder(MySpringProgram.class)
.profiles("profile1", "profile2")
.run(args);
}
}
Instead of activating the profile dynamically, you can put the profiles as vm-arguments in the catalina.sh
CATALINA_OPTS="-Dspring.profiles.active=dev"
System.setProperty("spring.profiles.active", "dev");
SpringApplication app =new SpringApplication(Application.class);
Properties props = new Properties();
props.put("AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAM", "dev");
app.setDefaultProperties(props);
app.run(args);
I think this code is better solution, because it doesn't set system property