001package org.lanyonm.playground.service;
002
003import java.util.Date;
004
005import org.slf4j.Logger;
006import org.slf4j.LoggerFactory;
007import org.springframework.stereotype.Service;
008
009@Service("exceptionService")
010public class ExceptionServiceImpl implements ExceptionService {
011
012        private Thread exceptionThread = null;
013        private volatile boolean running = true;
014        private static final Logger log = LoggerFactory.getLogger(ExceptionServiceImpl.class);
015
016        public void startThrowingExceptions() {
017                running = true;
018                if (exceptionThread == null) {
019                        exceptionThread = new Thread() {
020                                public void run() {
021                                        while (running) {
022                                                try {
023                                                        sleep(1000);
024                                                        throw new Exception("exception thrown at " + new Date());
025                                                } catch(Exception e) {
026                                                        log.error("exception thrown!", e);
027                                                }
028                                        }
029                                }
030                        };
031                        exceptionThread.start();
032                }
033        }
034
035        public void stopThrowingExceptions() {
036                if (exceptionThread != null) {
037                        try {
038                                running = false;
039                                exceptionThread.join();
040                                exceptionThread = null;
041                        } catch (InterruptedException e) {
042                                log.error("error joining thread after terminating it", e);
043                        }
044                }
045        }
046
047}