Skip to content

Commit

Permalink
Use ApplicationStartedEvent instead
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed May 14, 2020
1 parent bbd7711 commit 5ecd8d7
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
@@ -1,15 +1,25 @@
package com.izeye.throwaway.repository;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Repository;

import lombok.extern.slf4j.Slf4j;

/**
* Default {@link TestRepository}.
*
* @author Johnny Lim
*/
@Repository
@Slf4j
public class DefaultTestRepository implements TestRepository {

@PostConstruct
void initialize() {
log.info("@PostConstruct in DefaultTestRepository");
}

@Override
public String get(String id) {
throw new RuntimeException("Failed to get: " + id);
Expand Down
22 changes: 3 additions & 19 deletions src/main/java/com/izeye/throwaway/service/DefaultTestService.java
@@ -1,21 +1,19 @@
package com.izeye.throwaway.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.izeye.throwaway.repository.TestRepository;
import lombok.extern.slf4j.Slf4j;

/**
* Default {@link TestService}.
*
* @author Johnny Lim
*/
@Service
@Slf4j
public class DefaultTestService implements TestService {

private final TestRepository testRepository;
Expand All @@ -31,21 +29,7 @@ public String get(String id) {

@PostConstruct
void warmUp() {
List<String> ids = Arrays.asList("test1", "test2");
List<Thread> threads = new ArrayList<>();
ids.forEach((id) -> {
Thread thread = new Thread(() -> this.testRepository.get(id));
threads.add(thread);
thread.start();
});
threads.forEach(thread -> {
try {
thread.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
});
log.info("@PostConstruct in DefaultTestService");
}

}
@@ -0,0 +1,45 @@
package com.izeye.throwaway.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.izeye.throwaway.repository.TestRepository;
import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class WarmUpApplicationListener implements ApplicationListener<ApplicationStartedEvent> {

private final TestRepository testRepository;

public WarmUpApplicationListener(TestRepository testRepository) {
this.testRepository = testRepository;
}

@Override
public void onApplicationEvent(ApplicationStartedEvent event) {
log.info("In onApplicationEvent(): {}", event);

List<String> ids = Arrays.asList("test1", "test2");
List<Thread> threads = new ArrayList<>();
ids.forEach((id) -> {
Thread thread = new Thread(() -> this.testRepository.get(id));
threads.add(thread);
thread.start();
});
threads.forEach(thread -> {
try {
thread.join();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new RuntimeException(ex);
}
});
}

}

0 comments on commit 5ecd8d7

Please sign in to comment.