Added sample code for Spring events and Spring profiles.
This commit is contained in:
parent
fa4240311d
commit
b5cc62b50e
@ -33,7 +33,7 @@
|
||||
</dependency>
|
||||
|
||||
<!-- persistence -->
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
@ -118,7 +118,6 @@
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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 {
|
||||
|
||||
}
|
15
spring-all/src/main/resources/springProfiles-config.xml
Normal file
15
spring-all/src/main/resources/springProfiles-config.xml
Normal file
@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||
|
||||
<beans profile="dev">
|
||||
<bean id="devDatasourceConfig" class="org.baeldung.profiles.DevDatasourceConfig" />
|
||||
</beans>
|
||||
|
||||
<beans profile="production">
|
||||
<bean id="productionDatasourceConfig" class="org.baeldung.profiles.ProductionDatasourceConfig" />
|
||||
</beans>
|
||||
</beans>
|
26
spring-all/src/main/resources/springScheduled-config.xml
Normal file
26
spring-all/src/main/resources/springScheduled-config.xml
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||
|
||||
<context:property-placeholder location="classpath:springScheduled.properties" />
|
||||
|
||||
<!-- Configure the scheduler -->
|
||||
<task:scheduler id="myScheduler" pool-size="10" />
|
||||
|
||||
<bean id="myscheduler"
|
||||
class="org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler" />
|
||||
|
||||
<!-- Configure the fixedDealy, fixedRate or cron based schduled tasks -->
|
||||
<bean id="schedulingWithXmlConfig" class="org.baeldung.scheduling.SchedulingWithXmlConfig" />
|
||||
|
||||
<task:scheduled-tasks scheduler="myScheduler">
|
||||
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedDelayTask" fixed-delay="${fixedDelay.in.milliseconds}" initial-delay="1000" />
|
||||
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleFixedRateTask" fixed-rate="${fixedRate.in.milliseconds}" />
|
||||
<task:scheduled ref="schedulingWithXmlConfig" method="scheduleTaskUsingCronExpression" cron="${cron.expression}" />
|
||||
</task:scheduled-tasks>
|
||||
</beans>
|
3
spring-all/src/main/resources/springScheduled.properties
Normal file
3
spring-all/src/main/resources/springScheduled.properties
Normal file
@ -0,0 +1,3 @@
|
||||
cron.expression=0 15 10 15 * ?
|
||||
fixedRate.in.milliseconds=1000
|
||||
fixedDelay.in.milliseconds=1000
|
@ -0,0 +1,23 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("dev")
|
||||
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class DevProfileWithAnnotationTest {
|
||||
@Autowired
|
||||
DatasourceConfig datasourceConfig;
|
||||
|
||||
@Test
|
||||
public void testSpringProfiles() {
|
||||
Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ActiveProfiles("production")
|
||||
@ContextConfiguration(classes = { SpringProfilesConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ProductionProfileWithAnnotationTest {
|
||||
|
||||
@Autowired
|
||||
DatasourceConfig datasourceConfig;
|
||||
|
||||
@Autowired
|
||||
Environment environment;
|
||||
|
||||
@Test
|
||||
public void testSpringProfiles() {
|
||||
for (final String profileName : environment.getActiveProfiles()) {
|
||||
System.out.println("Currently active profile - " + profileName);
|
||||
}
|
||||
Assert.assertEquals("production", environment.getActiveProfiles()[0]);
|
||||
Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig);
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package org.baeldung.profiles;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
public class SpringProfilesWithXMLTest {
|
||||
|
||||
private ClassPathXmlApplicationContext classPathXmlApplicationContext;
|
||||
|
||||
@Test
|
||||
public void testSpringProfilesForDevEnvironment() {
|
||||
classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml");
|
||||
final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment();
|
||||
configurableEnvironment.setActiveProfiles("dev");
|
||||
classPathXmlApplicationContext.refresh();
|
||||
final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("devDatasourceConfig", DatasourceConfig.class);
|
||||
|
||||
Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpringProfilesForProdEnvironment() {
|
||||
classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml");
|
||||
final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment();
|
||||
configurableEnvironment.setActiveProfiles("production");
|
||||
classPathXmlApplicationContext.refresh();
|
||||
final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("productionDatasourceConfig", DatasourceConfig.class);
|
||||
|
||||
Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package org.baeldung.scheduling;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SpringSchedulingConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ScheduledAnnotationExampleTest {
|
||||
|
||||
@Test
|
||||
public void testScheduledAnnotation() throws InterruptedException {
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package org.baeldung.scheduling;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("classpath:springScheduled-config.xml")
|
||||
public class SchedulingWithXmlConfigTest {
|
||||
|
||||
@Test
|
||||
public void testXmlBasedScheduling() throws InterruptedException {
|
||||
Thread.sleep(5000);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package org.baeldung.springevents.asynchronous;
|
||||
|
||||
import org.baeldung.springevents.synchronous.CustomSpringEventPublisher;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { AsynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class AsynchronousCustomSpringEventsTest {
|
||||
|
||||
@Autowired
|
||||
private CustomSpringEventPublisher publisher;
|
||||
|
||||
@Test
|
||||
public void testCustomSpringEvents() throws InterruptedException {
|
||||
publisher.publishEvent("Hello world!!");
|
||||
System.out.println("Done publishing asynchronous custom event. ");
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.baeldung.springevents.synchronous.SynchronousSpringEventsConfig;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class ContextRefreshedListenerTest {
|
||||
|
||||
@Test
|
||||
public void testContextRefreshedListener() throws InterruptedException {
|
||||
System.out.println("Test context re-freshed listener.");
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package org.baeldung.springevents.synchronous;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { SynchronousSpringEventsConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
public class SynchronousCustomSpringEventsTest {
|
||||
|
||||
@Autowired
|
||||
private CustomSpringEventPublisher publisher;
|
||||
|
||||
@Test
|
||||
public void testCustomSpringEvents() throws InterruptedException {
|
||||
publisher.publishEvent("Hello world!!");
|
||||
System.out.println("Done publishing synchronous custom event. ");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user