Merge pull request #8974 from sk1418/master

[BAEL-3934]shedlock update
This commit is contained in:
Eric Martin 2020-03-29 18:52:16 -05:00 committed by GitHub
commit e2cdccff28
7 changed files with 78 additions and 23 deletions

View File

@ -52,7 +52,11 @@
<artifactId>shedlock-provider-jdbc-template</artifactId>
<version>${shedlock.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
</dependency>
<!-- Barcodes -->
<dependency>
<groupId>net.sourceforge.barbecue</groupId>
@ -79,6 +83,7 @@
<artifactId>javase</artifactId>
<version>${zxing.version}</version>
</dependency>
</dependencies>
<repositories>
@ -185,6 +190,7 @@
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
<modelmapper.version>2.3.2</modelmapper.version>
<problem-spring-web.version>0.23.0</problem-spring-web.version>
<h2.version>1.4.200</h2.version>
<shedlock.version>2.1.0</shedlock.version>
<barbecue.version>1.5-beta1</barbecue.version>
<barcode4j.version>2.1</barcode4j.version>

View File

@ -0,0 +1,15 @@
package com.baeldung;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.boot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class Application {
private static ApplicationContext applicationContext;
public static void main(String[] args) {
applicationContext = SpringApplication.run(Application.class, args);
}
}

View File

@ -5,9 +5,8 @@ import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
class TaskScheduler {
@Scheduled(cron = "*/15 * * * *")
class BaeldungTaskScheduler {
@Scheduled(cron = "0 0/15 * * * ?")
@SchedulerLock(name = "TaskScheduler_scheduledTask", lockAtLeastForString = "PT5M", lockAtMostForString = "PT14M")
public void scheduledTask() {
System.out.println("Running ShedLock task");

View File

@ -1,12 +1,16 @@
package com.baeldung.scheduling.shedlock;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.sql.DataSource;
@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
public class SchedulerConfiguration {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource);
}
}

View File

@ -0,0 +1,6 @@
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:mem:shedlock_DB;INIT=CREATE SCHEMA IF NOT EXISTS shedlock;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
username: sa
password:

View File

@ -0,0 +1,39 @@
package com.baeldung.scheduling.shedlock;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BaeldungTaskSchedulerIntegrationTest {
@Autowired
private BaeldungTaskScheduler taskScheduler;
@Test
public void whenShedLockConfigCorrect_thenSpringCtxtStartsWithoutError() {
// save the old out
PrintStream old = System.out;
// Create a stream to hold the output for test
ByteArrayOutputStream consoleOutput = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(consoleOutput);
System.setOut(ps);
//test
taskScheduler.scheduledTask();
System.out.flush();
String expected = "Running ShedLock task\n";
assertThat(consoleOutput.toString()).isEqualTo(expected);
//restore the old out
System.setOut(old);
}
}