Skip to content

Instantly share code, notes, and snippets.

public class TimePrinter {
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("HH:mm:ss");
private static final Logger LOGGER = LoggerFactory.getLogger(TimePrinter.class.getName());
public void reportCurrentTime() {
LOGGER.info("The time is now " + DATE_FORMAT.format(new Date()));
}
}
@Configuration
@EnableScheduling
@ComponentScan("pl.mjedynak")
public class AppConfig {
@Bean
public ScheduledTask scheduledTask() {
return new ScheduledTask();
}
public class App {
public static void main(String[] args) {
new AnnotationConfigApplicationContext(AppConfig.class);
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class ScheduledTaskIntegrationTest {
@Test
public void shouldInvokeTask() {
Callable<Boolean> threadWithNameExists =
() -> getAllStackTraces().keySet().stream().
anyMatch(t -> t.getName().equals(ScheduledTask.THREAD_NAME));
await().until(threadWithNameExists, is(true));
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean name="/AppConfig" class="pl.mjedynak.AppConfig" />
</beans>
public class Reader implements Runnable {
private final BlockingQueue<String> queue;
private final CountDownLatch latch;
public Reader(BlockingQueue<String> queue, CountDownLatch latch) {
this.queue = queue;
this.latch = latch;
}
public class LineProcessor implements Runnable {
private final PrimeFactorCounter primeFactorCounter = new PrimeFactorCounter();
private final BlockingQueue<String> inputQueue;
private final BlockingQueue<String> successQueue;
private final BlockingQueue<String> exceptionsQueue;
private final CountDownLatch inputLatch;
private final CountDownLatch outputLatch;
public class Writer implements Runnable {
private final String fileName;
private final BlockingQueue<String> queue;
private final CountDownLatch latch;
public Writer(BlockingQueue<String> queue, CountDownLatch latch, String fileName) {
this.queue = queue;
this.fileName = fileName;
public class Bootstrap {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final int THREAD_POOL_SIZE = 4;
static final String PRIME_FACTOR_FILE = "primeFactorCounter-threadPool.txt";
static final String EXCEPTIONS_FILE = "exceptions-threadPool.txt";
static final String SUMMARY_FILE = "summary-threadPool.txt";
public class ThreadPoolSystemTest {
@Before
public void setUp() {
new File(Bootstrap.PRIME_FACTOR_FILE).delete();
new File(Bootstrap.EXCEPTIONS_FILE).delete();
new File(Bootstrap.SUMMARY_FILE).delete();
}
@Test