Added sample code for Spring events and Spring profiles.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
public interface DatasourceConfig {
|
||||
public void setup();
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Profile("dev")
|
||||
public class DevDatasourceConfig implements DatasourceConfig {
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
System.out.println("Setting up datasource for DEV environment. ");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Profile("production")
|
||||
public class ProductionDatasourceConfig implements DatasourceConfig {
|
||||
|
||||
@Override
|
||||
public void setup() {
|
||||
System.out.println("Setting up datasource for PRODUCTION environment. ");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.profiles")
|
||||
public class SpringProfilesConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package org.baeldung.scheduling;
|
||||
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component("scheduledAnnotationExample")
|
||||
public class ScheduledAnnotationExample {
|
||||
|
||||
@Scheduled(fixedDelay = 1000)
|
||||
public void scheduleFixedDelayTask() {
|
||||
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString = "${fixedDelay.in.milliseconds}")
|
||||
public void scheduleFixedDelayTaskUsingExpression() {
|
||||
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000, initialDelay = 2000)
|
||||
public void scheduleFixedDelayWithInitialDelayTask() {
|
||||
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedRate = 1000)
|
||||
public void scheduleFixedRateTask() {
|
||||
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedRateString = "${fixedRate.in.milliseconds}")
|
||||
public void scheduleFixedRateTaskUsingExpression() {
|
||||
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 1000, initialDelay = 100)
|
||||
public void scheduleFixedRateWithInitialDelayTask() {
|
||||
System.out.println("Fixed delay task with one second initial delay - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scheduled task is executed at 10:15 AM on the 15th day of every month
|
||||
*/
|
||||
@Scheduled(cron = "0 15 10 15 * ?")
|
||||
public void scheduleTaskUsingCronExpression() {
|
||||
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
@Scheduled(cron = "${cron.expression}")
|
||||
public void scheduleTaskUsingExternalizedCronExpression() {
|
||||
System.out.println("schedule tasks using externalized cron expressions - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.baeldung.scheduling;
|
||||
|
||||
public class SchedulingWithXmlConfig {
|
||||
|
||||
public void scheduleFixedDelayTask() {
|
||||
System.out.println("Fixed delay task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
public void scheduleFixedRateTask() {
|
||||
System.out.println("Fixed rate task - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
|
||||
public void scheduleTaskUsingCronExpression() {
|
||||
System.out.println("schedule tasks using cron expressions - " + System.currentTimeMillis() / 1000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package org.baeldung.scheduling;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@ComponentScan("org.baeldung.scheduling")
|
||||
@PropertySource("classpath:springScheduled.properties")
|
||||
public class SpringSchedulingConfig {
|
||||
|
||||
@Bean
|
||||
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
|
||||
return new PropertySourcesPlaceholderConfigurer();
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package org.baeldung.springevents.asynchronous;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ApplicationEventMulticaster;
|
||||
import org.springframework.context.event.SimpleApplicationEventMulticaster;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.springevents.synchronous")
|
||||
public class AsynchronousSpringEventsConfig {
|
||||
|
||||
@Bean(name = "applicationEventMulticaster")
|
||||
public static ApplicationEventMulticaster simpleApplicationEventMulticaster() {
|
||||
final SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
|
||||
simpleApplicationEventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
return simpleApplicationEventMulticaster;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ContextRefreshedListener implements ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final ContextRefreshedEvent cse) {
|
||||
System.out.println("Handling context re-freshed event. ");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
public class CustomSpringEvent extends ApplicationEvent {
|
||||
private static final long serialVersionUID = -8053143381029977953L;
|
||||
|
||||
private String message;
|
||||
|
||||
public CustomSpringEvent(final Object source, final String message) {
|
||||
super(source);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CustomSpringEventListener implements ApplicationListener<CustomSpringEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(final CustomSpringEvent event) {
|
||||
System.out.println("Received spring custom event - " + event.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CustomSpringEventPublisher {
|
||||
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
public void publishEvent(final String message) {
|
||||
System.out.println("Publishing custom event. ");
|
||||
final CustomSpringEvent customSpringEvent = new CustomSpringEvent(this, message);
|
||||
applicationEventPublisher.publishEvent(customSpringEvent);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan("org.baeldung.springevents.synchronous")
|
||||
public class SynchronousSpringEventsConfig {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user