Skip to content

Commit fa42403

Browse files
author
eugenp
committed
small persistence fixes
1 parent 102ef52 commit fa42403

2 files changed

Lines changed: 64 additions & 10 deletions

File tree

core-java/src/test/java/org/baeldung/java/sandbox/SandboxJavaTest.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,75 @@
99
public class SandboxJavaTest {
1010

1111
@Test
12-
public void givenUsingTimer_whenSchedulingTaskOnce_thenCorrect() throws InterruptedException {
12+
public void givenUsingTimer_whenSchedulingTimerTaskOnce_thenCorrect() throws InterruptedException {
13+
final TimerTask timerTask = new TimerTask() {
14+
@Override
15+
public void run() {
16+
System.out.println("Time when was task performed" + new Date());
17+
System.out.println("Thread's name: " + Thread.currentThread().getName());
18+
}
19+
};
1320
final Timer timer = new Timer("Thread's name");
1421
System.out.println("Current time:" + new Date());
15-
System.out.println("Thread name: " + Thread.currentThread().getName());
22+
System.out.println("Thread's name: " + Thread.currentThread().getName());
23+
final long delay = 2L * 1000L;
24+
timer.schedule(timerTask, delay);
25+
Thread.sleep(delay);
26+
}
27+
28+
@Test
29+
public void givenUsingTimer_whenSchedulingRepeatedTask_thenCorrect() throws InterruptedException {
30+
final TimerTask repeatedTask = new TimerTask() {
31+
int count = 0;
1632

17-
final TimerTask timerTask = new TimerTask() {
1833
@Override
1934
public void run() {
20-
System.out.println("Time when task performed" + new Date());
21-
System.out.println("Thread name: " + Thread.currentThread().getName());
22-
timer.cancel();
35+
count++;
36+
System.out.println("Time when task was performed: " + new Date());
37+
System.out.println("Thread's name: " + Thread.currentThread().getName());
38+
if (count >= 5) {
39+
cancel();
40+
}
2341
}
2442
};
25-
final long delay = 2 * 1000;
26-
timer.schedule(timerTask, delay);
43+
final Timer timer = new Timer("Timer thread");
44+
System.out.println("Current time: " + new Date());
45+
System.out.println("Thread's name: " + Thread.currentThread().getName());
46+
final long delay = 2L * 1000L;
47+
final long period = 1L * 1000L;
48+
timer.scheduleAtFixedRate(repeatedTask, delay, period);
49+
Thread.sleep(delay + period * 5L);
50+
}
51+
52+
@Test
53+
public void givenUsingTimer_whenSchedulingRepeatedCustomTimerTask_thenCorrect() throws InterruptedException {
54+
class MyTask extends TimerTask {
55+
long timesToRun = 0;
56+
long timesRunned = 0;
57+
58+
public void setTimesToRun(final int timesToRun) {
59+
this.timesToRun = timesToRun;
60+
}
61+
62+
@Override
63+
public void run() {
64+
timesRunned++;
65+
System.out.println("Task performed on: " + new Date());
66+
System.out.println("Thread's name: " + Thread.currentThread().getName());
67+
if (timesRunned >= timesToRun) {
68+
cancel();
69+
}
70+
}
71+
}
72+
final MyTask repeatedTask = new MyTask();
73+
repeatedTask.setTimesToRun(5);
74+
final long delay = 2L * 1000L;
75+
final long period = 1L * 1000L;
76+
final Timer timer = new Timer("Timer");
77+
System.out.println("Current time: " + new Date());
78+
System.out.println("Thread's name: " + Thread.currentThread().getName());
79+
timer.scheduleAtFixedRate(repeatedTask, delay, period);
80+
Thread.sleep(delay + period * repeatedTask.timesToRun);
2781
}
2882

2983
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
################### DataSource Configuration ##########################
22
jdbc.driverClassName=com.mysql.jdbc.Driver
3-
jdbc.url=jdbc:mysql://localhost:3306/AUTHDATA
3+
jdbc.url=jdbc:mysql://localhost:3306/authdata?createDatabaseIfNotExist=true
44
jdbc.user=tutorialuser
55
jdbc.pass=tutorialmy5ql
66
init-db=false
77
################### Hibernate Configuration ##########################
88
hibernate.dialect=org.hibernate.dialect.MySQLDialect
99
hibernate.show_sql=true
10-
hibernate.hbm2ddl.auto=validate
10+
hibernate.hbm2ddl.auto=create-drop

0 commit comments

Comments
 (0)