From fa4240311ddab4a4bbfb317927f6286b145c254e Mon Sep 17 00:00:00 2001 From: eugenp Date: Sun, 28 Sep 2014 15:16:46 +0300 Subject: [PATCH] small persistence fixes --- .../java/sandbox/SandboxJavaTest.java | 72 ++++++++++++++++--- .../src/main/resources/application.properties | 4 +- 2 files changed, 65 insertions(+), 11 deletions(-) diff --git a/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java b/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java index f746ce63e2..5127ccb10c 100644 --- a/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java +++ b/core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java @@ -9,21 +9,75 @@ import org.junit.Test; public class SandboxJavaTest { @Test - public void givenUsingTimer_whenSchedulingTaskOnce_thenCorrect() throws InterruptedException { - final Timer timer = new Timer("Thread's name"); - System.out.println("Current time:" + new Date()); - System.out.println("Thread name: " + Thread.currentThread().getName()); - + public void givenUsingTimer_whenSchedulingTimerTaskOnce_thenCorrect() throws InterruptedException { final TimerTask timerTask = new TimerTask() { @Override public void run() { - System.out.println("Time when task performed" + new Date()); - System.out.println("Thread name: " + Thread.currentThread().getName()); - timer.cancel(); + System.out.println("Time when was task performed" + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); } }; - final long delay = 2 * 1000; + final Timer timer = new Timer("Thread's name"); + System.out.println("Current time:" + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + final long delay = 2L * 1000L; timer.schedule(timerTask, delay); + Thread.sleep(delay); + } + + @Test + public void givenUsingTimer_whenSchedulingRepeatedTask_thenCorrect() throws InterruptedException { + final TimerTask repeatedTask = new TimerTask() { + int count = 0; + + @Override + public void run() { + count++; + System.out.println("Time when task was performed: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + if (count >= 5) { + cancel(); + } + } + }; + final Timer timer = new Timer("Timer thread"); + System.out.println("Current time: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + final long delay = 2L * 1000L; + final long period = 1L * 1000L; + timer.scheduleAtFixedRate(repeatedTask, delay, period); + Thread.sleep(delay + period * 5L); + } + + @Test + public void givenUsingTimer_whenSchedulingRepeatedCustomTimerTask_thenCorrect() throws InterruptedException { + class MyTask extends TimerTask { + long timesToRun = 0; + long timesRunned = 0; + + public void setTimesToRun(final int timesToRun) { + this.timesToRun = timesToRun; + } + + @Override + public void run() { + timesRunned++; + System.out.println("Task performed on: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + if (timesRunned >= timesToRun) { + cancel(); + } + } + } + final MyTask repeatedTask = new MyTask(); + repeatedTask.setTimesToRun(5); + final long delay = 2L * 1000L; + final long period = 1L * 1000L; + final Timer timer = new Timer("Timer"); + System.out.println("Current time: " + new Date()); + System.out.println("Thread's name: " + Thread.currentThread().getName()); + timer.scheduleAtFixedRate(repeatedTask, delay, period); + Thread.sleep(delay + period * repeatedTask.timesToRun); } } diff --git a/spring-security-login-error-handling/src/main/resources/application.properties b/spring-security-login-error-handling/src/main/resources/application.properties index 70a8ae5daa..f80dba9ba5 100644 --- a/spring-security-login-error-handling/src/main/resources/application.properties +++ b/spring-security-login-error-handling/src/main/resources/application.properties @@ -1,10 +1,10 @@ ################### DataSource Configuration ########################## jdbc.driverClassName=com.mysql.jdbc.Driver -jdbc.url=jdbc:mysql://localhost:3306/AUTHDATA +jdbc.url=jdbc:mysql://localhost:3306/authdata?createDatabaseIfNotExist=true jdbc.user=tutorialuser jdbc.pass=tutorialmy5ql init-db=false ################### Hibernate Configuration ########################## hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.show_sql=true -hibernate.hbm2ddl.auto=validate +hibernate.hbm2ddl.auto=create-drop