diff --git a/src/main/java/com/github/yvasyliev/appenders/TelegramBotAppender.java b/src/main/java/com/github/yvasyliev/appenders/TelegramBotAppender.java index 51f39bc..64331c5 100644 --- a/src/main/java/com/github/yvasyliev/appenders/TelegramBotAppender.java +++ b/src/main/java/com/github/yvasyliev/appenders/TelegramBotAppender.java @@ -1,7 +1,6 @@ package com.github.yvasyliev.appenders; import com.github.yvasyliev.bots.telegram.notifier.TelegramNotifier; -import com.github.yvasyliev.config.TelegramNotifierConfig; import org.apache.logging.log4j.core.Appender; import org.apache.logging.log4j.core.Core; import org.apache.logging.log4j.core.Filter; @@ -15,7 +14,6 @@ import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.layout.PatternLayout; import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import java.io.IOException; import java.io.PrintWriter; @@ -25,13 +23,11 @@ @Plugin(name = "TelegramBotAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) public class TelegramBotAppender extends AbstractAppender { private final TelegramNotifier telegramNotifier; - private final int charactersLimit; protected TelegramBotAppender(String name, Filter filter, Layout layout, boolean ignoreExceptions, Property[] properties) { super(name, filter, layout, ignoreExceptions, properties); - var applicationContext = new AnnotationConfigApplicationContext(TelegramNotifierConfig.class); + var applicationContext = new AnnotationConfigApplicationContext("com.github.yvasyliev.bots.telegram.notifier"); this.telegramNotifier = applicationContext.getBean(TelegramNotifier.class); - this.charactersLimit = applicationContext.getBean("charactersLimit", int.class); } @PluginFactory @@ -50,13 +46,9 @@ public void append(LogEvent event) { %s""".formatted(formattedMessage, stackTrace); } - if (formattedMessage.length() > charactersLimit) { - formattedMessage = formattedMessage.substring(0, charactersLimit); - } - try { - telegramNotifier.notify(formattedMessage); - } catch (TelegramApiException e) { + telegramNotifier.applyWithException(formattedMessage); + } catch (Exception e) { e.printStackTrace(System.err); } } diff --git a/src/main/java/com/github/yvasyliev/bots/telegram/notifier/AdminNotifier.java b/src/main/java/com/github/yvasyliev/bots/telegram/notifier/AdminNotifier.java index 8fdd5e6..617827d 100644 --- a/src/main/java/com/github/yvasyliev/bots/telegram/notifier/AdminNotifier.java +++ b/src/main/java/com/github/yvasyliev/bots/telegram/notifier/AdminNotifier.java @@ -16,12 +16,18 @@ public class AdminNotifier extends AbstractRedTelBot implements TelegramNotifier ```""") private String messageTemplate; + @Value("4096") + private int charactersLimit; + public AdminNotifier(@Value("${BOT_TOKEN}") String botToken) { super(botToken); } @Override - public Message notify(String text) throws TelegramApiException { + public Message applyWithException(String text) throws TelegramApiException { + if (text.length() > charactersLimit) { + text = text.substring(0, charactersLimit); + } SendMessage sendMessage = SendMessage.builder() .chatId(getAdminId()) .text(messageTemplate.formatted(text)) diff --git a/src/main/java/com/github/yvasyliev/bots/telegram/notifier/TelegramNotifier.java b/src/main/java/com/github/yvasyliev/bots/telegram/notifier/TelegramNotifier.java index 88b09b1..d22b167 100644 --- a/src/main/java/com/github/yvasyliev/bots/telegram/notifier/TelegramNotifier.java +++ b/src/main/java/com/github/yvasyliev/bots/telegram/notifier/TelegramNotifier.java @@ -1,9 +1,8 @@ package com.github.yvasyliev.bots.telegram.notifier; +import org.springframework.util.function.ThrowingFunction; import org.telegram.telegrambots.meta.api.objects.Message; -import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @FunctionalInterface -public interface TelegramNotifier { - Message notify(String text) throws TelegramApiException; +public interface TelegramNotifier extends ThrowingFunction { } diff --git a/src/main/java/com/github/yvasyliev/config/RedTelBotConfiguration.java b/src/main/java/com/github/yvasyliev/config/RedTelBotConfiguration.java index 7abdf3b..e6528b4 100644 --- a/src/main/java/com/github/yvasyliev/config/RedTelBotConfiguration.java +++ b/src/main/java/com/github/yvasyliev/config/RedTelBotConfiguration.java @@ -24,12 +24,15 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; @Configuration @ComponentScan("com.github.yvasyliev.service") -public class RedTelBotConfiguration extends TelegramNotifierConfig { +public class RedTelBotConfiguration { @Autowired private JsonDeserializer postJsonDeserializer; @@ -111,4 +114,14 @@ public Map longStringMap(@Value("16") int maxSize) { public Map longExternalMessageDataMap(@Value("16") int maxSize) { return synchronizedFixedSizeMap(maxSize); } + + @Bean + public Executor delayedExecutor() { + return CompletableFuture.delayedExecutor(10, TimeUnit.SECONDS, singleThreadExecutor()); + } + + @Bean + public Executor singleThreadExecutor() { + return Executors.newSingleThreadExecutor(); + } } diff --git a/src/main/java/com/github/yvasyliev/config/TelegramNotifierConfig.java b/src/main/java/com/github/yvasyliev/config/TelegramNotifierConfig.java deleted file mode 100644 index 719b47a..0000000 --- a/src/main/java/com/github/yvasyliev/config/TelegramNotifierConfig.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.github.yvasyliev.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -@Configuration -@ComponentScan("com.github.yvasyliev.bots.telegram.notifier") -public class TelegramNotifierConfig { - @Bean - public Executor delayedExecutor() { - return CompletableFuture.delayedExecutor(10, TimeUnit.SECONDS, singleThreadExecutor()); - } - - @Bean - public Executor singleThreadExecutor() { - return Executors.newSingleThreadExecutor(); - } - - @Bean - public int charactersLimit() { - return 4096; - } -}