|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2018 Slava Semushin <[email protected]> |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | + */ |
| 18 | +package ru.mystamps.web.service; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.time.StopWatch; |
| 21 | + |
| 22 | +import org.slf4j.Logger; |
| 23 | + |
| 24 | +import lombok.RequiredArgsConstructor; |
| 25 | + |
| 26 | +import ru.mystamps.web.service.dto.DownloadResult; |
| 27 | + |
| 28 | +@RequiredArgsConstructor |
| 29 | +public class TimedDownloaderService implements DownloaderService { |
| 30 | + |
| 31 | + private final Logger log; |
| 32 | + private final DownloaderService service; |
| 33 | + |
| 34 | + // @todo #665 TimedDownloaderService: add unit tests |
| 35 | + @Override |
| 36 | + public DownloadResult download(String url) { |
| 37 | + // Why we don't use Spring's StopWatch? |
| 38 | + // 1) because its javadoc says that it's not intended for production |
| 39 | + // 2) because we don't want to have strong dependencies on the Spring Framework |
| 40 | + StopWatch timer = new StopWatch(); |
| 41 | + |
| 42 | + // start() and stop() may throw IllegalStateException and in this case it's possible |
| 43 | + // that we won't generate anything or lose already generated result. I don't want to |
| 44 | + // make method body too complicated by adding many try/catches and I believe that such |
| 45 | + // exception will never happen because it would mean that we're using API in a wrong way. |
| 46 | + timer.start(); |
| 47 | + DownloadResult result = service.download(url); |
| 48 | + timer.stop(); |
| 49 | + |
| 50 | + if (result.hasSucceeded()) { |
| 51 | + log.debug( |
| 52 | + "{} bytes have been downloaded in {} msecs", |
| 53 | + result.getData().length, |
| 54 | + timer.getTime() |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | +} |
0 commit comments