Skip to content

Commit d7cf77d

Browse files
author
Artem
committed
Properly start db writers
1 parent 80c5f55 commit d7cf77d

3 files changed

Lines changed: 23 additions & 6 deletions

File tree

src/main/java/com/codenameart/rocketmerger/Webhook.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import com.codenameart.rocketmerger.q.DBQueue;
44
import com.codenameart.rocketmerger.q.DBWriter;
55
import lombok.extern.log4j.Log4j2;
6+
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.beans.factory.annotation.Autowired;
78
import org.springframework.boot.SpringApplication;
89
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
10+
import org.springframework.context.ApplicationContext;
911
import org.springframework.context.annotation.ComponentScan;
1012
import org.springframework.http.HttpStatus;
1113
import org.springframework.stereotype.Controller;
@@ -16,11 +18,13 @@
1618
import javax.annotation.PostConstruct;
1719
import java.util.List;
1820
import java.util.TimeZone;
21+
import java.util.concurrent.ExecutorService;
22+
import java.util.concurrent.Executors;
1923

2024
/**
2125
* Created by Artem on 18.12.2017.
2226
*/
23-
@Log4j2
27+
@Slf4j
2428
@Controller
2529
@EnableAutoConfiguration
2630
@ComponentScan
@@ -32,6 +36,9 @@ public class Webhook {
3236
@Autowired
3337
DBWriter dbWriter;
3438

39+
@Autowired
40+
private ApplicationContext context;
41+
3542
public static void main(String[] args) throws Exception {
3643
SpringApplication.run(Webhook.class, args);
3744
}
@@ -40,13 +47,16 @@ public static void main(String[] args) throws Exception {
4047
@ResponseStatus(HttpStatus.OK)
4148
void process(@RequestBody List<Message> payload) {
4249
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
50+
log.debug("Message received");
4351
payload.stream().map(Message::getMessage).forEach(dbQueue::offer);
4452
}
4553

4654
@PostConstruct
4755
public void startDBLoop() {
56+
ExecutorService executorService = Executors.newFixedThreadPool(5);
4857
for (int i = 0; i < 10; i++) {
49-
dbWriter.startLoop();
58+
DBWriter bean = context.getBean(DBWriter.class);
59+
executorService.execute(bean);
5060
}
5161
}
5262
}

src/main/java/com/codenameart/rocketmerger/q/DBQueue.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public boolean offer(Pokemon pokemon) {
2323
public Pokemon poll() {
2424
return queue.poll();
2525
}
26+
27+
public int size() {
28+
return queue.size();
29+
}
2630
}

src/main/java/com/codenameart/rocketmerger/q/DBWriter.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import com.codenameart.rocketmerger.Pokemon;
44
import com.codenameart.rocketmerger.PokemonRepository;
5+
import lombok.extern.slf4j.Slf4j;
56
import org.springframework.beans.factory.annotation.Autowired;
6-
import org.springframework.scheduling.annotation.Async;
77

88
import java.util.Date;
99

1010
/**
1111
* Created by Artem on 23.12.2017.
1212
*/
13-
public class DBWriter {
13+
@Slf4j
14+
public class DBWriter implements Runnable {
1415
@Autowired
1516
DBQueue queue;
1617

@@ -20,8 +21,9 @@ public class DBWriter {
2021
PokemonRepository pokemonRepository;
2122

2223

23-
@Async
24-
public void startLoop(){
24+
@Override
25+
public void run() {
26+
log.info("Starting queue DBWriter");
2527
while (true) {
2628
if (Thread.currentThread().isInterrupted()) {
2729
return;
@@ -36,6 +38,7 @@ public void startLoop(){
3638
}
3739
} else {
3840
pokemon.setLast_modified(new Date());
41+
log.info("Queue length: " + queue.size());
3942
pokemonRepository.save(pokemon);
4043
}
4144
}

0 commit comments

Comments
 (0)